ppcrslidesannotated

99
Solving Difficult LR Parsing Conflicts by Postponing Them Luis Garcia-Forte and Casiano Rodriguez-Leon Universidad de La Laguna CoRTA 2010 September 09-10, 2010

Upload: casiano-rodriguez-leon

Post on 18-Dec-2014

155 views

Category:

Technology


0 download

DESCRIPTION

Though yacc-like LR parser generators provide ways to solve shift-reduceconflicts using token precedences, no mechanisms are providedfor the resolution of difficult reduce-reduce conflicts. To solve this kindof conflicts the language designer has to modify the grammar.

TRANSCRIPT

Page 1: Ppcrslidesannotated

Solving Difficult LR Parsing Conflicts byPostponing Them

Luis Garcia-Forte and Casiano Rodriguez-LeonUniversidad de La Laguna

CoRTA2010 September 09-10, 2010

Page 2: Ppcrslidesannotated

Outline

Introduction

Context Dependent Parsing

Nested Parsing

Conclusions

Page 3: Ppcrslidesannotated

Outline

Introduction

Context Dependent Parsing

Nested Parsing

Conclusions

Page 4: Ppcrslidesannotated

Yacc Limitations

� Yacc-like LR parser generators provide ways to solveshift-reduce mechanisms based on token precedence.

� No mechanisms are provided for the resolution ofreduce-reduce conflicts or difficult shift-reduce conflicts.

� To solve such kind of conflicts the language designer hasto modify the grammar.

Page 5: Ppcrslidesannotated

Yacc Limitations

� Yacc-like LR parser generators provide ways to solveshift-reduce mechanisms based on token precedence.

� No mechanisms are provided for the resolution ofreduce-reduce conflicts or difficult shift-reduce conflicts.

� To solve such kind of conflicts the language designer hasto modify the grammar.

Page 6: Ppcrslidesannotated

Yacc Limitations

� Yacc-like LR parser generators provide ways to solveshift-reduce mechanisms based on token precedence.

� No mechanisms are provided for the resolution ofreduce-reduce conflicts or difficult shift-reduce conflicts.

� To solve such kind of conflicts the language designer hasto modify the grammar.

Page 7: Ppcrslidesannotated

Postponed Conflict Resolution Approach

� The strategy presented here extends yacc conflictresolution mechanisms with new ones, supplying ways toresolve conflicts that can not be solved using staticprecedences.

� The algorithm for the generation of the LR tables remainsunchanged, but the programmer can modify the parsingtables during run time.

Page 8: Ppcrslidesannotated

Postponed Conflict Resolution Approach

� The strategy presented here extends yacc conflictresolution mechanisms with new ones, supplying ways toresolve conflicts that can not be solved using staticprecedences.

� The algorithm for the generation of the LR tables remainsunchanged, but the programmer can modify the parsingtables during run time.

Page 9: Ppcrslidesannotated

Postponed Conflict Resolution Approach

� The strategy presented here extends yacc conflictresolution mechanisms with new ones, supplying ways toresolve conflicts that can not be solved using staticprecedences.

� The algorithm for the generation of the LR tables remainsunchanged, but the programmer can modify the parsingtables during run time.

Page 10: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑and

� B → β↑when the lookahead token is@.

Page 11: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑and

� B → β↑when the lookahead token is@.

Page 12: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑and

� B → β↑when the lookahead token is@.

Page 13: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑and

� B → β↑when the lookahead token is@.

Page 14: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑

and� B → β↑

when the lookahead token is@.

Page 15: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑and

� B → β↑when the lookahead token is@.

Page 16: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑and

� B → β↑

when the lookahead token is@.

Page 17: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑and

� B → β↑when the lookahead token is

@.

Page 18: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Identify the conflict: What LR(0)-items/productions andtokens are involved?.

� Tools must support that stage, as for example via the.output file generated by eyapp.

� Suppose we identify that the participants are the twoLR(0)-items

� A→ α↑and

� B → β↑when the lookahead token is@.

Page 19: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� The software must allow the use of symbolic labels to referby name to the productions involved in the conflict.

� In Eyapp names and labels can be explicitly given usingthe directive %name, using a syntax similar to this one:

%name :rA A → α

%name :rB B → β

Page 20: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� The software must allow the use of symbolic labels to referby name to the productions involved in the conflict.

� In Eyapp names and labels can be explicitly given usingthe directive %name, using a syntax similar to this one:

%name :rA A → α

%name :rB B → β

Page 21: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� The software must allow the use of symbolic labels to referby name to the productions involved in the conflict.

� In Eyapp names and labels can be explicitly given usingthe directive %name, using a syntax similar to this one:

%name :rA A → α

%name :rB B → β

Page 22: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Give a symbolic name to the conflict. In this case wechoose isAorB as name of the conflict.

� Inside the body section of the grammar, mark the points ofconflict using the new reserved word %PREC followed bythe conflict name:

%name :rA A → α %PREC IsAorB

%name :rA B → β %PREC IsAorB

Page 23: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Give a symbolic name to the conflict. In this case wechoose isAorB as name of the conflict.

� Inside the body section of the grammar, mark the points ofconflict using the new reserved word %PREC followed bythe conflict name:

%name :rA A → α %PREC IsAorB

%name :rA B → β %PREC IsAorB

Page 24: Ppcrslidesannotated

PPCR Approach: Identify the Conflict

� Give a symbolic name to the conflict. In this case wechoose isAorB as name of the conflict.

� Inside the body section of the grammar, mark the points ofconflict using the new reserved word %PREC followed bythe conflict name:

%name :rA A → α %PREC IsAorB

%name :rA B → β %PREC IsAorB

Page 25: Ppcrslidesannotated

Conflict Identification: Shift-Reduce Conflicts

The procedure is similar for shift-reduce conflicts.

� Let us assume we have identified a shift-reduce conflictbetween LR-(0) items

� A→ α↑and

� B → β ↑ γ for some token ’@’.� Again, we must give a symbolic name to A→ α and mark

with the new %PREC directive the places where the conflictoccurs:

%name :rA A → α %PREC IsAorB

B → β %PREC IsAorB γ

Page 26: Ppcrslidesannotated

Conflict Identification: Shift-Reduce Conflicts

The procedure is similar for shift-reduce conflicts.� Let us assume we have identified a shift-reduce conflict

between LR-(0) items

� A→ α↑and

� B → β ↑ γ for some token ’@’.� Again, we must give a symbolic name to A→ α and mark

with the new %PREC directive the places where the conflictoccurs:

%name :rA A → α %PREC IsAorB

B → β %PREC IsAorB γ

Page 27: Ppcrslidesannotated

Conflict Identification: Shift-Reduce Conflicts

The procedure is similar for shift-reduce conflicts.� Let us assume we have identified a shift-reduce conflict

between LR-(0) items� A→ α↑

and� B → β ↑ γ for some token ’@’.

� Again, we must give a symbolic name to A→ α and markwith the new %PREC directive the places where the conflictoccurs:

%name :rA A → α %PREC IsAorB

B → β %PREC IsAorB γ

Page 28: Ppcrslidesannotated

Conflict Identification: Shift-Reduce Conflicts

The procedure is similar for shift-reduce conflicts.� Let us assume we have identified a shift-reduce conflict

between LR-(0) items� A→ α↑

and

� B → β ↑ γ for some token ’@’.� Again, we must give a symbolic name to A→ α and mark

with the new %PREC directive the places where the conflictoccurs:

%name :rA A → α %PREC IsAorB

B → β %PREC IsAorB γ

Page 29: Ppcrslidesannotated

Conflict Identification: Shift-Reduce Conflicts

The procedure is similar for shift-reduce conflicts.� Let us assume we have identified a shift-reduce conflict

between LR-(0) items� A→ α↑

and� B → β ↑ γ

for some token ’@’.� Again, we must give a symbolic name to A→ α and mark

with the new %PREC directive the places where the conflictoccurs:

%name :rA A → α %PREC IsAorB

B → β %PREC IsAorB γ

Page 30: Ppcrslidesannotated

Conflict Identification: Shift-Reduce Conflicts

The procedure is similar for shift-reduce conflicts.� Let us assume we have identified a shift-reduce conflict

between LR-(0) items� A→ α↑

and� B → β ↑ γ for some token ’@’.

� Again, we must give a symbolic name to A→ α and markwith the new %PREC directive the places where the conflictoccurs:

%name :rA A → α %PREC IsAorB

B → β %PREC IsAorB γ

Page 31: Ppcrslidesannotated

Conflict Identification: Shift-Reduce Conflicts

The procedure is similar for shift-reduce conflicts.� Let us assume we have identified a shift-reduce conflict

between LR-(0) items� A→ α↑

and� B → β ↑ γ for some token ’@’.

� Again, we must give a symbolic name to A→ α and markwith the new %PREC directive the places where the conflictoccurs:

%name :rA A → α %PREC IsAorB

B → β %PREC IsAorB γ

Page 32: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� Introduce a %conflict directive inside the head sectionof the translation scheme to specify the way the conflict willbe solved.

� The directive is followed by some code - known as theconflict handler - whose mission is to modify the parsingtables.

� This code will be executed each time the associatedconflict state is reached.

Page 33: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� Introduce a %conflict directive inside the head sectionof the translation scheme to specify the way the conflict willbe solved.

� The directive is followed by some code - known as theconflict handler - whose mission is to modify the parsingtables.

� This code will be executed each time the associatedconflict state is reached.

Page 34: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� Introduce a %conflict directive inside the head sectionof the translation scheme to specify the way the conflict willbe solved.

� The directive is followed by some code - known as theconflict handler - whose mission is to modify the parsingtables.

� This code will be executed each time the associatedconflict state is reached.

Page 35: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� Introduce a %conflict directive inside the head sectionof the translation scheme to specify the way the conflict willbe solved.

� The directive is followed by some code - known as theconflict handler - whose mission is to modify the parsingtables.

� This code will be executed each time the associatedconflict state is reached.

Page 36: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� This is the usual layout of the conflict handler for areduce-reduce conflict:

%conflict IsAorB {if (is_A) {$self->YYSetReduce(’@’, ’:rA’ );

}else {$self->YYSetReduce(’@’, ’:rB’ );

}}

Inside a conflict code handler the Perl default variable $_refers to the input and $self refers to the parser object.

Page 37: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

�� This is the usual layout of the conflict handler for areduce-reduce conflict:

%conflict IsAorB {if (is_A) {$self->YYSetReduce(’@’, ’:rA’ );

}else {$self->YYSetReduce(’@’, ’:rB’ );

}}

Inside a conflict code handler the Perl default variable $_refers to the input and $self refers to the parser object.

Page 38: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

�� This is the usual layout of the conflict handler for areduce-reduce conflict:

%conflict IsAorB {if (is_A) {$self->YYSetReduce(’@’, ’:rA’ );

}else {$self->YYSetReduce(’@’, ’:rB’ );

}}

Inside a conflict code handler the Perl default variable $_refers to the input and $self refers to the parser object.

Page 39: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

�� This is the usual layout of the conflict handler for ashift-reduce conflict:

%conflict IsAorB {if (is_A) {$self->YYSetReduce(’@’, ’:rA’ );

}else {$self->YYSetShift(’@’);

}}

Page 40: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� The method YYSetReduce receives a list of tokens and aproduction label, like :rA. The call

$self->YYSetReduce(’@’, ’:rA’ )

sets the parsing action for the state associated with theconflict IsAorB to reduce by the production :rA when thecurrent lookahead is @.

� The method YYSetShift receives a token (or a referenceto a list of tokens). The call

$self->YYSetShift(’@’)

sets the parsing action for the state associated with theconflict IsAorB to shift when the current lookahead is @.

Page 41: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� The method YYSetReduce receives a list of tokens and aproduction label, like :rA. The call

$self->YYSetReduce(’@’, ’:rA’ )

sets the parsing action for the state associated with theconflict IsAorB to reduce by the production :rA when thecurrent lookahead is @.

� The method YYSetShift receives a token (or a referenceto a list of tokens). The call

$self->YYSetShift(’@’)

sets the parsing action for the state associated with theconflict IsAorB to shift when the current lookahead is @.

Page 42: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� The method YYSetReduce receives a list of tokens and aproduction label, like :rA. The call

$self->YYSetReduce(’@’, ’:rA’ )

sets the parsing action for the state associated with theconflict IsAorB to reduce by the production :rA when thecurrent lookahead is @.

� The method YYSetShift receives a token (or a referenceto a list of tokens). The call

$self->YYSetShift(’@’)

sets the parsing action for the state associated with theconflict IsAorB to shift when the current lookahead is @.

Page 43: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� %conflict IsAorB {if (is_A) { $self->YYSetReduce(’@’, ’:rA’ );}else { $self->YYSetReduce(’@’, ’:rB’ ); }

}

� The call to is_A represents the context-dependentdynamic knowledge that allows us to take the rightdecision.

� It is usually a call to a nested parser to check that A→ αapplies but it can also be any other contextual informationwe have to determine which one is the right production.

Page 44: Ppcrslidesannotated

PPCR Approach: The Conflict Handler

� %conflict IsAorB {if (is_A) { $self->YYSetReduce(’@’, ’:rA’ );}else { $self->YYSetReduce(’@’, ’:rB’ ); }

}

� The call to is_A represents the context-dependentdynamic knowledge that allows us to take the rightdecision.

� It is usually a call to a nested parser to check that A→ αapplies but it can also be any other contextual informationwe have to determine which one is the right production.

Page 45: Ppcrslidesannotated

Outline

Introduction

Context Dependent Parsing

Nested Parsing

Conclusions

Page 46: Ppcrslidesannotated

A Simple Example: Context Dependant Associativity

$ cat input_for_dynamicgrammar.txt2-1-1 # left: 0 = (2-1)-1

RIGHT

2-1-1 # right: 2 = 2-(1-1)

LEFT

3-1-1 # left: 1 = (3-1)-1

RIGHT3-1-1 # right: 3 = 3-(1-1)

Page 47: Ppcrslidesannotated

A Simple Example: Context Dependant Associativity

$ cat input_for_dynamicgrammar.txt2-1-1 # left: 0 = (2-1)-1

RIGHT

2-1-1 # right: 2 = 2-(1-1)

LEFT

3-1-1 # left: 1 = (3-1)-1

RIGHT3-1-1 # right: 3 = 3-(1-1)

Page 48: Ppcrslidesannotated

A Simple Example: Context Dependant Associativity

$ cat input_for_dynamicgrammar.txt2-1-1 # left: 0 = (2-1)-1

RIGHT

2-1-1 # right: 2 = 2-(1-1)

LEFT

3-1-1 # left: 1 = (3-1)-1

RIGHT3-1-1 # right: 3 = 3-(1-1)

Page 49: Ppcrslidesannotated

A Simple Example: Context Dependant Associativity

$ cat input_for_dynamicgrammar.txt2-1-1 # left: 0 = (2-1)-1

RIGHT

2-1-1 # right: 2 = 2-(1-1)

LEFT

3-1-1 # left: 1 = (3-1)-1

RIGHT3-1-1 # right: 3 = 3-(1-1)

Page 50: Ppcrslidesannotated

A Simple Example: Context Dependant Associativity

$ cat input_for_dynamicgrammar.txt2-1-1 # left: 0 = (2-1)-1

RIGHT

2-1-1 # right: 2 = 2-(1-1)

LEFT

3-1-1 # left: 1 = (3-1)-1

RIGHT3-1-1 # right: 3 = 3-(1-1)

Page 51: Ppcrslidesannotated

A Simple Example: Context Dependant Associativity

$ cat input_for_dynamicgrammar.txt2-1-1 # left: 0 = (2-1)-1

RIGHT

2-1-1 # right: 2 = 2-(1-1)

LEFT

3-1-1 # left: 1 = (3-1)-1

RIGHT3-1-1 # right: 3 = 3-(1-1)

Page 52: Ppcrslidesannotated

A Simple Example: Bodyp: c * {};

c: $expr { print "$expr\n" }| RIGHT { $reduce = 0}| LEFT { $reduce = 1}

;

expr: ’(’ $expr ’)’ { $expr }| NUM

| %name :Mexpr.left %PREC leftORright’-’ expr.right %PREC leftORright

{ $left - $right }

;%%

Page 53: Ppcrslidesannotated

A Simple Example: Bodyp: c * {};

c: $expr { print "$expr\n" }| RIGHT { $reduce = 0}| LEFT { $reduce = 1}

;

expr: ’(’ $expr ’)’ { $expr }| NUM

| %name :Mexpr.left %PREC leftORright’-’ expr.right %PREC leftORright

{ $left - $right }

;%%

Page 54: Ppcrslidesannotated

A Simple Example: Bodyp: c * {};

c: $expr { print "$expr\n" }| RIGHT { $reduce = 0}| LEFT { $reduce = 1}

;

expr: ’(’ $expr ’)’ { $expr }| NUM

| %name :Mexpr.left %PREC leftORright’-’ expr.right %PREC leftORright

{ $left - $right }

;%%

Page 55: Ppcrslidesannotated

A Simple Example: Bodyp: c * {};

c: $expr { print "$expr\n" }| RIGHT { $reduce = 0}| LEFT { $reduce = 1}

;

expr: ’(’ $expr ’)’ { $expr }| NUM

| %name :Mexpr.left %PREC leftORright’-’ expr.right %PREC leftORright

{ $left - $right }

;%%

Page 56: Ppcrslidesannotated

A Simple Example: Bodyp: c * {};

c: $expr { print "$expr\n" }| RIGHT { $reduce = 0}| LEFT { $reduce = 1}

;

expr: ’(’ $expr ’)’ { $expr }| NUM

| %name :Mexpr.left %PREC leftORright’-’ expr.right %PREC leftORright

{ $left - $right }

;%%

Page 57: Ppcrslidesannotated

A Simple Example: Head Section$ cat dynamicgrammar.eyp

%{my $reduce = 1;%}

%whites /(\s*(?:#.*)?\s*)/%token NUM = /(\d+)/

%conflict leftORright {if ($reduce) { $self->YYSetReduce(’-’, ’:M’) }else { $self->YYSetShift(’-’) }

}

%expect 1

%%

Page 58: Ppcrslidesannotated

A Simple Example: Head Section$ cat dynamicgrammar.eyp

%{my $reduce = 1;%}

%whites /(\s*(?:#.*)?\s*)/%token NUM = /(\d+)/

%conflict leftORright {if ($reduce) { $self->YYSetReduce(’-’, ’:M’) }else { $self->YYSetShift(’-’) }

}

%expect 1

%%

Page 59: Ppcrslidesannotated

A Simple Example: Head Section$ cat dynamicgrammar.eyp

%{my $reduce = 1;%}

%whites /(\s*(?:#.*)?\s*)/%token NUM = /(\d+)/

%conflict leftORright {if ($reduce) { $self->YYSetReduce(’-’, ’:M’) }else { $self->YYSetShift(’-’) }

}

%expect 1

%%

casiano
casiano
Automatically GeneratedLexical Analyzer
casiano
casiano
casiano
Page 60: Ppcrslidesannotated

A Simple Example: Head Section$ cat dynamicgrammar.eyp

%{my $reduce = 1;%}

%whites /(\s*(?:#.*)?\s*)/%token NUM = /(\d+)/

%conflict leftORright {if ($reduce) { $self->YYSetReduce(’-’, ’:M’) }else { $self->YYSetShift(’-’) }

}

%expect 1

%%

Page 61: Ppcrslidesannotated

A Simple Example: Head Section$ cat dynamicgrammar.eyp

%{my $reduce = 1;%}

%whites /(\s*(?:#.*)?\s*)/%token NUM = /(\d+)/

%conflict leftORright {if ($reduce) { $self->YYSetReduce(’-’, ’:M’) }else { $self->YYSetShift(’-’) }

}

%expect 1

%%

Page 62: Ppcrslidesannotated

Outline

Introduction

Context Dependent Parsing

Nested Parsing

Conclusions

Page 63: Ppcrslidesannotated

Enumerated and Subrange Types in Pascal

� The problem is taken from the Bison manual.

type subrange = lo .. hi;type enum = (a, b, c);

� The Pascal standard allows only numeric literals andconstant identifiers for the subrange bounds (lo and hi),but Extended Pascal and many other implementationsallow arbitrary expressions there.

type subrange = (a) .. b;type enum = (a);

� To aggravate the conflict we have added the C commaoperator to the language

type subrange = (a, b, c) .. (d, e);type enum = (a, b, c);

Page 64: Ppcrslidesannotated

Enumerated and Subrange Types in Pascal

� The problem is taken from the Bison manual.

type subrange = lo .. hi;type enum = (a, b, c);

� The Pascal standard allows only numeric literals andconstant identifiers for the subrange bounds (lo and hi),but Extended Pascal and many other implementationsallow arbitrary expressions there.

type subrange = (a) .. b;type enum = (a);

� To aggravate the conflict we have added the C commaoperator to the language

type subrange = (a, b, c) .. (d, e);type enum = (a, b, c);

Page 65: Ppcrslidesannotated

Enumerated and Subrange Types in Pascal

� The problem is taken from the Bison manual.

type subrange = lo .. hi;type enum = (a, b, c);

� The Pascal standard allows only numeric literals andconstant identifiers for the subrange bounds (lo and hi),but Extended Pascal and many other implementationsallow arbitrary expressions there.

type subrange = (a) .. b;type enum = (a);

� To aggravate the conflict we have added the C commaoperator to the language

type subrange = (a, b, c) .. (d, e);type enum = (a, b, c);

Page 66: Ppcrslidesannotated

Enumerated and Subrange Types in Pascal

� The problem is taken from the Bison manual.

type subrange = lo .. hi;type enum = (a, b, c);

� The Pascal standard allows only numeric literals andconstant identifiers for the subrange bounds (lo and hi),but Extended Pascal and many other implementationsallow arbitrary expressions there.

type subrange = (a) .. b;type enum = (a);

� To aggravate the conflict we have added the C commaoperator to the language

type subrange = (a, b, c) .. (d, e);type enum = (a, b, c);

casiano
casiano
casiano
Can't decide up to here
casiano
Page 67: Ppcrslidesannotated

Enumerated vs Range: Full Grammar%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%%type_decl : ’TYPE’ ID ’=’ type ’;’ ;

type : ’(’ id_list ’)’ | expr ’..’ expr ;

id_list : ID | id_list ’,’ ID ;expr : ID

| NUM | ’(’ expr ’)’| expr ’,’ expr /* new */| expr ’+’ expr | expr ’-’ expr| expr ’*’ expr | expr ’/’ expr

;

Page 68: Ppcrslidesannotated

Enumerated vs Range: Full Grammar%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%%type_decl : ’TYPE’ ID ’=’ type ’;’ ;

type : ’(’ id_list ’)’ | expr ’..’ expr ;

id_list : ID | id_list ’,’ ID ;expr : ID

| NUM | ’(’ expr ’)’| expr ’,’ expr /* new */| expr ’+’ expr | expr ’-’ expr| expr ’*’ expr | expr ’/’ expr

;

Page 69: Ppcrslidesannotated

Enumerated vs Range: Full Grammar%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%%type_decl : ’TYPE’ ID ’=’ type ’;’ ;

type : ’(’ id_list ’)’ | expr ’..’ expr ;

id_list : ID | id_list ’,’ ID ;expr : ID

| NUM | ’(’ expr ’)’| expr ’,’ expr /* new */| expr ’+’ expr | expr ’-’ expr| expr ’*’ expr | expr ’/’ expr

;

Page 70: Ppcrslidesannotated

Enumerated vs Range: Full Grammar%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%%type_decl : ’TYPE’ ID ’=’ type ’;’ ;

type : ’(’ id_list ’)’ | expr ’..’ expr ;

id_list : ID | id_list ’,’ ID ;expr : ID

| NUM | ’(’ expr ’)’| expr ’,’ expr /* new */| expr ’+’ expr | expr ’-’ expr| expr ’*’ expr | expr ’/’ expr

;

Page 71: Ppcrslidesannotated

Enumerated vs Range: Full Grammar%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%%type_decl : ’TYPE’ ID ’=’ type ’;’ ;

type : ’(’ id_list ’)’ | expr ’..’ expr ;

id_list : ID | id_list ’,’ ID ;expr : ID

| NUM | ’(’ expr ’)’| expr ’,’ expr /* new */| expr ’+’ expr | expr ’-’ expr| expr ’*’ expr | expr ’/’ expr

;

casiano
These are the productions involved in the conflict
casiano
casiano
casiano
casiano
Page 72: Ppcrslidesannotated

Identifying the Conflict$ eyapp -v pascalenumeratedvsrange.eyp2 reduce/reduce conflicts

State 11:id_list -> ID . (Rule 4)expr -> ID . (Rule 12)

’)’ [reduce using rule 12 (expr)]’)’ reduce using rule 4 (id_list)’,’ [reduce using rule 12 (expr)]’,’ reduce using rule 4 (id_list)

’*’ reduce using rule 12 (expr)’+’ reduce using rule 12 (expr)’-’ reduce using rule 12 (expr)’/’ reduce using rule 12 (expr)

Page 73: Ppcrslidesannotated

Identifying the Conflict$ eyapp -v pascalenumeratedvsrange.eyp2 reduce/reduce conflicts

State 11:id_list -> ID . (Rule 4)expr -> ID . (Rule 12)

’)’ [reduce using rule 12 (expr)]’)’ reduce using rule 4 (id_list)’,’ [reduce using rule 12 (expr)]’,’ reduce using rule 4 (id_list)

’*’ reduce using rule 12 (expr)’+’ reduce using rule 12 (expr)’-’ reduce using rule 12 (expr)’/’ reduce using rule 12 (expr)

Page 74: Ppcrslidesannotated

Enumerated vs Range: Body

type_decl : ’type’ ID ’=’ type ’;’ ;

type :%name ENUMLookahead ’(’ id_list ’)’

| %name RANGELookahead expr ’..’ expr

;Lookahead: /* empty */

{ $is_range = $_[0]->YYPreParse(’Range’); };

Page 75: Ppcrslidesannotated

Enumerated vs Range: Body

type_decl : ’type’ ID ’=’ type ’;’ ;

type :%name ENUMLookahead ’(’ id_list ’)’

| %name RANGELookahead expr ’..’ expr

;

Lookahead: /* empty */{ $is_range = $_[0]->YYPreParse(’Range’); }

;

Page 76: Ppcrslidesannotated

Enumerated vs Range: Body

type_decl : ’type’ ID ’=’ type ’;’ ;

type :%name ENUMLookahead ’(’ id_list ’)’

| %name RANGELookahead expr ’..’ expr

;Lookahead: /* empty */{ $is_range = $_[0]->YYPreParse(’Range’); }

;

Page 77: Ppcrslidesannotated

Nested Parsing: Future Work

$ cat Range2.eyp

%from pascalnestedeyapp2 import expr%%range: expr ’..’ expr ’;’;%%

Page 78: Ppcrslidesannotated

Nested Parsing: Current State$ cat Range.eyp%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%%range: expr ’..’ expr ’;’;

expr : ’(’ expr ’)’| expr ’+’ expr | expr ’-’ expr| expr ’*’ expr | expr ’/’ expr| expr ’,’ expr | ID | NUM

;%%

Page 79: Ppcrslidesannotated

Enumerated vs Range: Body

id_list :%name ID:ENUMID %PREC rangeORenum

| id_list ’,’ ID;expr : ’(’ expr ’)’ { $_[2] } /* bypass */

| %name ID:RANGEID %PREC rangeORenum

| %name PLUS expr ’+’ expr| %name MINUS expr ’-’ expr| %name TIMES expr ’*’ expr| %name DIV expr ’/’ expr| %name COMMA expr ’,’ expr| %name NUM NUM

;

%%

Page 80: Ppcrslidesannotated

Enumerated vs Range: Bodyid_list :

%name ID:ENUMID %PREC rangeORenum

| id_list ’,’ ID;

expr : ’(’ expr ’)’ { $_[2] } /* bypass */| %name ID:RANGE

ID %PREC rangeORenum| %name PLUS expr ’+’ expr| %name MINUS expr ’-’ expr| %name TIMES expr ’*’ expr| %name DIV expr ’/’ expr| %name COMMA expr ’,’ expr| %name NUM NUM

;

%%

Page 81: Ppcrslidesannotated

Enumerated vs Range: Bodyid_list :

%name ID:ENUMID %PREC rangeORenum

| id_list ’,’ ID;expr : ’(’ expr ’)’ { $_[2] } /* bypass */

| %name ID:RANGEID %PREC rangeORenum

| %name PLUS expr ’+’ expr| %name MINUS expr ’-’ expr| %name TIMES expr ’*’ expr| %name DIV expr ’/’ expr| %name COMMA expr ’,’ expr| %name NUM NUM

;

%%

Page 82: Ppcrslidesannotated

Enumerated vs Range: Header

%{my $is_range = 0;

%}%conflict rangeORenum {if ($is_range){ $self->YYSetReduce([’,’, ’)’], ’ID:RANGE’ ); }else{ $self->YYSetReduce([’,’, ’)’], ’ID:ENUM’ ); }

}

%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%expect-rr 2%%

Page 83: Ppcrslidesannotated

Enumerated vs Range: Header%{my $is_range = 0;

%}%conflict rangeORenum {if ($is_range){ $self->YYSetReduce([’,’, ’)’], ’ID:RANGE’ ); }else{ $self->YYSetReduce([’,’, ’)’], ’ID:ENUM’ ); }

}

%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%expect-rr 2%%

Page 84: Ppcrslidesannotated

Enumerated vs Range: Header%{my $is_range = 0;

%}%conflict rangeORenum {if ($is_range){ $self->YYSetReduce([’,’, ’)’], ’ID:RANGE’ ); }else{ $self->YYSetReduce([’,’, ’)’], ’ID:ENUM’ ); }

}

%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%expect-rr 2%%

Page 85: Ppcrslidesannotated

Enumerated vs Range: Header%{my $is_range = 0;

%}%conflict rangeORenum {if ($is_range){ $self->YYSetReduce([’,’, ’)’], ’ID:RANGE’ ); }else{ $self->YYSetReduce([’,’, ’)’], ’ID:ENUM’ ); }

}

%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%expect-rr 2%%

Page 86: Ppcrslidesannotated

Enumerated vs Range: Header%{my $is_range = 0;

%}%conflict rangeORenum {if ($is_range){ $self->YYSetReduce([’,’, ’)’], ’ID:RANGE’ ); }else{ $self->YYSetReduce([’,’, ’)’], ’ID:ENUM’ ); }

}

%token ID = /([A-Za-z]\w*)/%token NUM = /(\d+)/

%left ’,’%left ’-’ ’+’%left ’*’ ’/’

%expect-rr 2%%

Page 87: Ppcrslidesannotated

Enumerated vs Range: Execution

$ eyapp -P Range.eyp

$ eyapp -TC pascalnestedeyapp2.eyp

$ ./pascalnestedeyapp2.pm -t -i -m 1 \-c ’type e = (x, y, z);’

typeDecl_is_type_ID_type(TERMINAL[e],ENUM(idList_is_idList_ID(

idList_is_idList_ID(ID(TERMINAL[x]),TERMINAL[y]),

TERMINAL[z])))

Page 88: Ppcrslidesannotated

Enumerated vs Range: Execution

$ eyapp -P Range.eyp

$ eyapp -TC pascalnestedeyapp2.eyp

$ ./pascalnestedeyapp2.pm -t -i -m 1 \-c ’type e = (x, y, z);’

typeDecl_is_type_ID_type(TERMINAL[e],ENUM(idList_is_idList_ID(

idList_is_idList_ID(ID(TERMINAL[x]),TERMINAL[y]),

TERMINAL[z])))

Page 89: Ppcrslidesannotated

Enumerated vs Range: Execution

$ eyapp -P Range.eyp

$ eyapp -TC pascalnestedeyapp2.eyp

$ ./pascalnestedeyapp2.pm -t -i -m 1 \-c ’type e = (x, y, z);’

typeDecl_is_type_ID_type(TERMINAL[e],ENUM(idList_is_idList_ID(

idList_is_idList_ID(ID(TERMINAL[x]),TERMINAL[y]),

TERMINAL[z])))

Page 90: Ppcrslidesannotated

Enumerated vs Range: Execution

$ eyapp -P Range.eyp

$ eyapp -TC pascalnestedeyapp2.eyp

$ ./pascalnestedeyapp2.pm -t -i -m 1 \-c ’type e = (x, y, z);’

typeDecl_is_type_ID_type(TERMINAL[e],ENUM(idList_is_idList_ID(

idList_is_idList_ID(ID(TERMINAL[x]),TERMINAL[y]),

TERMINAL[z])))

Page 91: Ppcrslidesannotated

Outline

Introduction

Context Dependent Parsing

Nested Parsing

Conclusions

Page 92: Ppcrslidesannotated

Conclusions

� The new conflict resolution mechanisms provide ways toresolve conflicts that can not be solved using staticprecedences.

� They also provides finer control over the conflict resolutionprocess than existing alternatives, like GLR andbacktracking LR.

� There are no limitations to PPCR parsing, since the conflicthandler is implemented in a universal language and it thencan resort to any kind of nested parsing algorithm.

� The conflict resolution mechanisms presented here can beintroduced in any LR parsing tools.

� It certainly involves more programmer work than branchingmethods like GLR or backtracking LR.

Page 93: Ppcrslidesannotated

Conclusions

� The new conflict resolution mechanisms provide ways toresolve conflicts that can not be solved using staticprecedences.

� They also provides finer control over the conflict resolutionprocess than existing alternatives, like GLR andbacktracking LR.

� There are no limitations to PPCR parsing, since the conflicthandler is implemented in a universal language and it thencan resort to any kind of nested parsing algorithm.

� The conflict resolution mechanisms presented here can beintroduced in any LR parsing tools.

� It certainly involves more programmer work than branchingmethods like GLR or backtracking LR.

Page 94: Ppcrslidesannotated

Conclusions

� The new conflict resolution mechanisms provide ways toresolve conflicts that can not be solved using staticprecedences.

� They also provides finer control over the conflict resolutionprocess than existing alternatives, like GLR andbacktracking LR.

� There are no limitations to PPCR parsing, since the conflicthandler is implemented in a universal language and it thencan resort to any kind of nested parsing algorithm.

� The conflict resolution mechanisms presented here can beintroduced in any LR parsing tools.

� It certainly involves more programmer work than branchingmethods like GLR or backtracking LR.

Page 95: Ppcrslidesannotated

Conclusions

� The new conflict resolution mechanisms provide ways toresolve conflicts that can not be solved using staticprecedences.

� They also provides finer control over the conflict resolutionprocess than existing alternatives, like GLR andbacktracking LR.

� There are no limitations to PPCR parsing, since the conflicthandler is implemented in a universal language and it thencan resort to any kind of nested parsing algorithm.

� The conflict resolution mechanisms presented here can beintroduced in any LR parsing tools.

� It certainly involves more programmer work than branchingmethods like GLR or backtracking LR.

Page 96: Ppcrslidesannotated

Conclusions

� The new conflict resolution mechanisms provide ways toresolve conflicts that can not be solved using staticprecedences.

� They also provides finer control over the conflict resolutionprocess than existing alternatives, like GLR andbacktracking LR.

� There are no limitations to PPCR parsing, since the conflicthandler is implemented in a universal language and it thencan resort to any kind of nested parsing algorithm.

� The conflict resolution mechanisms presented here can beintroduced in any LR parsing tools.

� It certainly involves more programmer work than branchingmethods like GLR or backtracking LR.

Page 97: Ppcrslidesannotated

Conclusions

� The new conflict resolution mechanisms provide ways toresolve conflicts that can not be solved using staticprecedences.

� They also provides finer control over the conflict resolutionprocess than existing alternatives, like GLR andbacktracking LR.

� There are no limitations to PPCR parsing, since the conflicthandler is implemented in a universal language and it thencan resort to any kind of nested parsing algorithm.

� The conflict resolution mechanisms presented here can beintroduced in any LR parsing tools.

� It certainly involves more programmer work than branchingmethods like GLR or backtracking LR.

Page 98: Ppcrslidesannotated

http://parse-eyapp.googlecode.com/svn/branches/PPCR

Thanks!

Page 99: Ppcrslidesannotated

http://parse-eyapp.googlecode.com/svn/branches/PPCR

Thanks!