written 8.4 years ago by | • modified 8.4 years ago |
Mumbai University > Computer Engineering > Sem6 > System Programming and Compiler Construction
Marks: 10M
Year: May 2015
written 8.4 years ago by | • modified 8.4 years ago |
Mumbai University > Computer Engineering > Sem6 > System Programming and Compiler Construction
Marks: 10M
Year: May 2015
written 8.4 years ago by | • modified 8.4 years ago |
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: $\$$ <• b and b •> $\$$. 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.
Example: The input string:
$id_1 + id_2 * id_3$
after inserting precedence relations becomes
$\$$ <• id1 •> + <• id2•>* <• id3 •> $\$$
Having precedence relations allows to identify handles as follows:
-Scan the string from left until seeing •>
-Scan backwards the string from right to left until seeing <•
-Everything between the two relations <• and •> forms the handle.
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
If $ is on the top of the stack and ip points to $ then return
Else
Let a be the top terminal on the stack, and b the symbol pointed to by ip
If a<• b or a =• b then
Push b onto the stack
Advance ip to the next input symbol
Else if a •>bthen
repeat
Pop the stack
until the top stack terminal is related by <•
to the terminal most recently popped
else error ()
end
Making Operator Precedence Relations
Algorithm for Constructing Precedence Functions
Create a directed graph whose nodes are in the groups, next for each symbols a and b do: place an edge from the group of $g_b$ to the group of fa if a <• b, otherwise if a •>b place an edge from the group of fa to that of $g_b$;
If the constructed graph has a cycle then no precedence functions exist. When there are no cycles collect the length of the longest paths from the groups of fa and gb respectively.
Example: Consider the above table
Using the algorithm leads to the following graph:
From which we extract the following precedence functions:
- | id | + | * | $ |
---|---|---|---|---|
f | 4 | 2 | 4 | 0 |
g | 5 | 1 | 3 | 0 |
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:
These error diagnostic issued at handling of errors during reduction.
During handling of shift/reduce errors; the diagnostic’s issues are:
Advantages:
Disadvantages:
Since the relationship between a grammar for the language being parsed and the operator precedence parser itself is tenuous, one cannot always be sure the parser accepts exactly the desired language.
Only a small class of grammars can be parsed using operator-precedence techniques.