written 8.6 years ago by | • modified 4.8 years ago |
E → E + E E → E * E E → id
Apply operator precedence parsing algorithm for the statement
Id - id * id
written 8.6 years ago by | • modified 4.8 years ago |
E → E + E E → E * E E → id
Apply operator precedence parsing algorithm for the statement
Id - id * id
written 8.6 years ago by | • modified 8.6 years ago |
• Bottom-up parsers for a large class of context-free grammars can be easily developed using operator grammars.
• Operator grammars have the property that no production right side is empty or has two adjacent nonterminal.
• This property enables the implementation of efficient operator-precedence parsers.
• These parser rely on the following three precedence relations: | Relation | Meaning | |----------|--------------------------------| | a,<· b | a yields precedence to b | | a,=·,b | a has the same precedence as b | | a,·>,b | a takes precedence over b |
• These operator precedence relations allow to delimit the handles in the right sentential forms: <• marks the left end, =• appears in the interior of the handle, and •> marks the right end.
• Let assume that between the symbols ai and ai+1 there is exactly one precedence relation. Suppose that $ is the end of the string.
• Then for all terminals we can write: $ \lt• b and b •\gt $. If we remove all nonterminal and place the correct precedence relation: <•, =•, •> between the remaining terminals, there remain strings that can be analyzed by easily developed parser.
• For example, the following operator precedence relations can be introduced for simple expressions:
Stack | Input | a | b | Action |
---|---|---|---|---|
$ | id + id $ | $ | id | Shift id | |
$id | + id $ | id | + | Reduce E -> id | |
$ E | + id $ | $ | + | Shift + | |
$E+ | id $ | + | id | Shift id | |
$E+id | $ | id | $ | Reduce E -> id | |
$E+E | $ | + | $ | Reduce E -> E+E | |
$E | $ | $ | $ | Accept |
Example: The input string:
id1 + id2 * id3
After inserting precedence relations becomes
$ \lt d1 \gt + \lt id2\gt* \lt id3\gt $
Having precedence relations allows to identify handles as follows:
Note that not the entire sentential form is scanned to find the handle.
Operator Precedence Parsing Algorithm
Initialize: Set ip to point to the first symbol of w$
Repeat: Let X be the top stack symbol, and a the symbol pointed to by ip.
Making Operator Precedence Relations
Algorithm for Constructing Precedence Functions
Example: Consider the above table
Using the algorithm leads to the following graph:
From which we extract the following precedence functions:
Error Recovery in operator-precedence parsing:
There are two points in the parsing process at which an operator-precedence parser can discover syntactic error:
The error checker does the following errors:
During handling of shift/reduce errors; the diagnostic’s issues are:
Advantages:
Disadvantages: