COMPILER PREDICTIVE PARSER
-
Upload
varunrocks93 -
Category
Documents
-
view
234 -
download
0
Transcript of COMPILER PREDICTIVE PARSER
-
8/11/2019 COMPILER PREDICTIVE PARSER
1/145
Oxford University Press 2013. All rights reserved.
K. Muneeswaran
Professor and HeadDepartment of Computer Science and
EngineeringMepco Schlenk Engineering College,
SivakasiEmail: [email protected]
1
COMPILER DESIGN
mailto:[email protected]:[email protected] -
8/11/2019 COMPILER PREDICTIVE PARSER
2/145
Oxford University Press 2013. All rights reserved.
CHAPTER 4
Syntax AnalysisIn these slides, we will cover the following topics:
Introduction
Context-free grammar and structure of a language Parser and its types
Top-down parser
Bottom-up parser
Implementation
Parser generator tool
Error handling
2
-
8/11/2019 COMPILER PREDICTIVE PARSER
3/145
Oxford University Press 2013. All rights reserved.
INTRODUCTION
Syntax Vs Semantics analysis
Choice of grammar for the power, flexibility and easeof implementation should be traded off
-
8/11/2019 COMPILER PREDICTIVE PARSER
4/145
Oxford University Press 2013. All rights reserved.
Relation among Grammar, Language
and Recognizer
-
8/11/2019 COMPILER PREDICTIVE PARSER
5/145
Oxford University Press 2013. All rights reserved.
Context free grammar (CFG) for syntax
analysis
1. CFG is powerful enough to represent the structureof the programming language
2. It does not look into the context or semanticsassociated with the language representation
3. It is relatively easy to implement compared to
context sensitive grammar or unrestricted grammar
-
8/11/2019 COMPILER PREDICTIVE PARSER
6/145
Oxford University Press 2013. All rights reserved.
CFG Gis defined as a 4-tuple:G = (VT, VN, R, S)
1. VNis a finite set of non-terminalsymbols or variables2. VTis a finite set of terminalsymbols, disjoint with VN,
which makes up the actual content of the sentence3. Ris a relation from VNto(VNU VT). The members of R
are calledproduction rulesorrewriting rules4. Sis the startvariable, used to represent the whole
sentence(or program)
Denoting CFG
A, where A is a non-terminal and is string ofgrammar symbols (VNor VT)
-
8/11/2019 COMPILER PREDICTIVE PARSER
7/145
Oxford University Press 2013. All rights reserved.
CFGAn Example
S aSbSS bSaSS
OrS aSbS | bSaS |
Where Sis the non-terminal and start symbola,bare terminals and is the empty symbol
In many cases, the grammar is recursivelydefined for representingthe language
-
8/11/2019 COMPILER PREDICTIVE PARSER
8/145
Oxford University Press 2013. All rights reserved.
Representations of Grammar and
Examples
CFG can be recognized by the abstract machine, calledpushdown automaton.
Derivation (Production):is the process of deriving the given sentence of the
language from the start symbol
-
8/11/2019 COMPILER PREDICTIVE PARSER
9/145
Oxford University Press 2013. All rights reserved.
Example Grammar:exp exp + exp | exp * exp | id
Sentence: id * id + id
Derivations:exp exp * exp
exp * exp + exp
exp * exp + idid * exp + id id * id + id
Derivations can be left most or rightmostSentential form: consists of terminals and non-terminals in the derivationprocessSentence: has only of terminalsIn general:StartSymbolOne or more Sentential Forms Sentence
-
8/11/2019 COMPILER PREDICTIVE PARSER
10/145
Oxford University Press 2013. All rights reserved.
Parse Trees:are the diagrammatic representation of the
derivation steps
Interior Node: Non-terminalLeaf Node: Terminal
-
8/11/2019 COMPILER PREDICTIVE PARSER
11/145
Oxford University Press 2013. All rights reserved.
Limitations of Context Free Grammar
The semantics of the language could not be handled by CFG
Example:int a,b,c;a = 10;
b = 20;c = a + b;
The semantics such as whether the variables are declared could notbe incorporated as part of the syntax specifications.
This type of language is denoted by:L = {wcw | w is string of alphabets}
-
8/11/2019 COMPILER PREDICTIVE PARSER
12/145
Oxford University Press 2013. All rights reserved.
Ambiguities in Grammar and resolving
them
Ambiguousgrammar is a grammar, which gives morethan one parse tree for at least one sentence w.
For example, the grammar:exp exp + exp | exp * exp | idis ambiguous grammar
-
8/11/2019 COMPILER PREDICTIVE PARSER
13/145
Oxford University Press 2013. All rights reserved.
Eliminating ambiguity from grammar
1. Eliminating ambiguity due to lack of precedence andassociativity
2. Eliminating dangling else ambiguity
-
8/11/2019 COMPILER PREDICTIVE PARSER
14/145
Oxford University Press 2013. All rights reserved.
Steps for eliminating ambiguity due to
lack of precedence and associativity
1. Add n new non-terminal where n is the number ofprecedence levels.
2. Define the least precedence operators for the startsymbol.
S S operator11 S | S operator12 S | ..
where S is the start symbol and operator11,operator12, etc are operators having least precedence.
-
8/11/2019 COMPILER PREDICTIVE PARSER
15/145
Oxford University Press 2013. All rights reserved.
3. For each new non-terminal, rules are defined using theoperators in next level of precedence.
AA operator21 A | A operator22 A |
where A is a new non-terminal and operator21,operator22 etc are operators at same level ofprecedence
4. At the end, the definitions in the given grammar without
precedence are added to the remaining new non-terminal
Steps for eliminating ambiguity due to lack
of precedence and associativity ..contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
16/145
Oxford University Press 2013. All rights reserved.
5. To include the associativity, in each definition
a. If the associativity is left to right, then write the definition in leftrecursive form :
AA operator1 B | A operator2 B |
b. If the associativity is right to left, then write the definition in rightrecursive form
AB operator1 A | B operator2 A |.
where A is the non-terminal which defines operators at a particularlevel of precedence and B is then on-terminal which defines operatorsat next higher level of precedence.
Steps for eliminating ambiguity due to lack
of precedence and associativity ..contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
17/145
Oxford University Press 2013. All rights reserved.
Eliminating ambiguity due to lack of precedence
and associativity An Example
Grammar: exp exp + exp | exp * exp | (exp) | id |const
Two levels operator precedence (+,*) leads to addition of
two new non-terminals termand fact.expr expr + exprterm term * termfact (expr) | id | const
expr expr + termterm term * factfact (expr) | id | const
-
8/11/2019 COMPILER PREDICTIVE PARSER
18/145
Oxford University Press 2013. All rights reserved.
Eliminating dangling else ambiguity
stmt if con then stmt | if con then stmt else stmt |other
Ambiguities resolved grammar
stmt matched | unmatchedmatched if con then matched else matched | otherunmatched if con then stmt | if con then matched
else unmatched
-
8/11/2019 COMPILER PREDICTIVE PARSER
19/145
Oxford University Press 2013. All rights reserved.
Role of the Parser
process of grouping the sequence of tokensin the sourceprogram for checking the syntax(structure of the language)by constructing a parse tree
Also performs error handling
Two methods:Top down parsing (left most derivation)Bottom up parsing(reverse of right most derivation)
-
8/11/2019 COMPILER PREDICTIVE PARSER
20/145
Oxford University Press 2013. All rights reserved.
Issues in the design of Parser
Choice for developing parser such as:High level languages like CLow level languages
Using parser generator tools.
What intermediate code to produce
Handling error
-
8/11/2019 COMPILER PREDICTIVE PARSER
21/145
-
8/11/2019 COMPILER PREDICTIVE PARSER
22/145
Oxford University Press 2013. All rights reserved.
1. Recursive descendant parsing with backtracking
2. Recursive descendant parsing without backtracking
Recursive descendant parsing
Recursion:Left Recursive grammar (AA | )
Right Recursive grammar (A A | )
Left recursion has to be eliminated since leads toinfinite loop
-
8/11/2019 COMPILER PREDICTIVE PARSER
23/145
Oxford University Press 2013. All rights reserved.
Elimination of left recursion
AA1 | A2 | .. | An | 1 | 2 | . | m
A1 A1 | 2 A1 | . | m A1A1 1 A1 | 2 A1 | .. | n A1 |
Rewritten as
-
8/11/2019 COMPILER PREDICTIVE PARSER
24/145
-
8/11/2019 COMPILER PREDICTIVE PARSER
25/145
Oxford University Press 2013. All rights reserved.
Recursive-Descendent Parser with
backtrackingAn Example
Grammar:S AB
A c | cBB d
Given String: cdd
-
8/11/2019 COMPILER PREDICTIVE PARSER
26/145
Oxford University Press 2013. All rights reserved.
Recursive-Descendent Parserwith
backtrackingPseudo Procedure
returnStatus procedure S();{parseStatus = SUCCESS
parseStatus = A();if(parserStatus == ERR){
report(Error);
return parseStatus;}
parseStatus = B();if(parserStatus == ERR){
report(Error);return parseStatus;
}parseStatus = SUCCESS;
return parseStatus;}
-
8/11/2019 COMPILER PREDICTIVE PARSER
27/145
Oxford University Press 2013. All rights reserved.
returnStatus Procedure A(){if (getInput() == c)
return SUCCESS;else if( getInput() == c)//undo any actions
associated //with the parsing
of previous //rule alternative
return( B() );}
returnStatus Procedure B(){
if(getInput() == d)Return
SUCCESS;else
Return FAIL;}
Recursive-Descendent Parserwith
backtrackingPseudo ProcedureContd
-
8/11/2019 COMPILER PREDICTIVE PARSER
28/145
Oxford University Press 2013. All rights reserved.
Common Prefix and its elimination
(left factoring)
A1| 2| | n| 1| 2| . m
AB | 1| 2| . mB 1| 2| | n
-
8/11/2019 COMPILER PREDICTIVE PARSER
29/145
Oxford University Press 2013. All rights reserved.
Common Prefix and its elimination
(left factoring) contd
exp exp + exp | exp * exp | id
exp exp B | idB + exp | * exp
-
8/11/2019 COMPILER PREDICTIVE PARSER
30/145
Oxford University Press 2013. All rights reserved.
Predictive Parser
Predictive Parser has 5 major components:
1. A input buffer to hold the sentence to be parsed2. An output buffer to display the actions taken by the
parser3. Stack data structure used to hold the grammar
symbols during the parsing process4. Predictive parsing table
5. Parser program
-
8/11/2019 COMPILER PREDICTIVE PARSER
31/145
-
8/11/2019 COMPILER PREDICTIVE PARSER
32/145
Oxford University Press 2013. All rights reserved.
ip= 1a = buffer[ip]X =top (stack)do{
if (X = = b )if (X = = a){
ip=ip+1
pop (stack)}else
return -1
Algorithm- Predictive_parser(w#) contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
33/145
Oxford University Press 2013. All rights reserved.
else if (X = = A)if( M(X,a) = X Y1Y2..Yn){
Pop (stack)for each grammar symbol Y
j
in Y1 Yn-1
,Yn
doPush (stack, Yj)
}else
return -1
}while(X # and buffer[ip] #)
return 1
Algorithm- Predictive_parser(w#) contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
34/145
Oxford University Press 2013. All rights reserved.
Error conditions in predictive parsing
1. The top of the stack terminal is not matching thenext input symbol.
2. The referred predictive parsing tables entry isblank
-
8/11/2019 COMPILER PREDICTIVE PARSER
35/145
Oxford University Press 2013. All rights reserved.
Predictive ParserAn Example
expr term expr1expr1+ term expr1| term fact term1term1* fact term1| fact (expr) | id | const
-
8/11/2019 COMPILER PREDICTIVE PARSER
36/145
Oxford University Press 2013. All rights reserved.
Predictive Parsing table for Arithmetic
expression
Parsing action using the predictive
-
8/11/2019 COMPILER PREDICTIVE PARSER
37/145
Oxford University Press 2013. All rights reserved.
Parsing action using the predictive
parsing table for the string: id * id + id
Parsing action using the predictive
-
8/11/2019 COMPILER PREDICTIVE PARSER
38/145
Oxford University Press 2013. All rights reserved.
Parsing action using the predictive
parsing table for the string: id * id + id
contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
39/145
Oxford University Press 2013. All rights reserved.
Parsing action using the predictive
parsing table for the string: id * + id
-
8/11/2019 COMPILER PREDICTIVE PARSER
40/145
Oxford University Press 2013. All rights reserved.
// G the context free grammar given as input// first(X) returns a set of terminals for a grammar symbol X// first() returns a set of terminals for a string of grammar
symbol ()
// follow(A) returns a set of terminals for a non-terminal A// returns M(A,a) the predictive parsing table with m x nelements
// where m is the number of Non-terminals in G
// n is the number of terminals plus one
AlgorithmconstructPredictiveTable(G)
-
8/11/2019 COMPILER PREDICTIVE PARSER
41/145
Oxford University Press 2013. All rights reserved.
for each production Ain G do{
for each terminal a in first() doM(A,a) = A
if(is in first())for each terminal b in follow(A) do
M(A,b) = Aif(# is in follow(A))
M(A,#) = A}
AlgorithmconstructPredictiveTable(G)
contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
42/145
Oxford University Press 2013. All rights reserved.
// returnsset of terminals that begin strings derived from X{
if(X is a terminal)first(X) = {X}
if(X is non-terminal and X Y1Y2..Yn)
{ if(is in first(Y1), first(Y2) . first(Yk) )first(X) = first(X) first(Y1) . first(Yk+1){}if (k ==n)
first(X) = first(X) {}}
return first(X)}
Algorithmfirst(X)
Algorithm follow (A)
-
8/11/2019 COMPILER PREDICTIVE PARSER
43/145
Oxford University Press 2013. All rights reserved.
// S start symbol// returnsset of terminals that follow A in the body of the production
{If (A == S)
follow (A) = follow(A) {#}if( B Ais a production in G){
follow(A) = follow(A) first(){}if(is in first())
follow(A) = follow(A) follow(B)}if( B A is a production in G)
follow(A) = follow(A) follow(B)return follow(A)}
Algorithmfollow (A)
-
8/11/2019 COMPILER PREDICTIVE PARSER
44/145
Oxford University Press 2013. All rights reserved.
1. expr term expr1
2. expr1 + term expr1|
3. term fact term1
4. term1 * fact term1|
5. fact (expr) | id | cons
AlgorithmconstructPredictiveTable(G)
Example1
-
8/11/2019 COMPILER PREDICTIVE PARSER
45/145
Oxford University Press 2013. All rights reserved.
Computations of FirstandFollow
Al ith t tP di ti T bl (G)
-
8/11/2019 COMPILER PREDICTIVE PARSER
46/145
Oxford University Press 2013. All rights reserved.
AlgorithmconstructPredictiveTable(G)
Example2
S Aa | bAc | Bc | bBaAdB d
-
8/11/2019 COMPILER PREDICTIVE PARSER
47/145
Oxford University Press 2013. All rights reserved.
Limitations of top-down parser
Requires preprocessing steps such as:Elimination of left recursionLeft factoring
The effect of backtracking has to be considered
-
8/11/2019 COMPILER PREDICTIVE PARSER
48/145
Oxford University Press 2013. All rights reserved.
Bottom up Parser
It is the process of finding the exact right hand side of thesentential form (handle) to reduce to the previous rightsentential form in the right most derivation in reverse
The main actions are:ShiftReduce
AcceptError
-
8/11/2019 COMPILER PREDICTIVE PARSER
49/145
Oxford University Press 2013. All rights reserved.
Simple stack based parser
Shift:In this action the next input symbol is pushed into the stack
Reduce:If there is a handle in the top of stack then this handlewhich is the body of a production is popped from the stack and theequivalent non-terminal in the head of the production is pushedinto the stack
Accept:Parsing is terminated successfully when the top of thestack has # and start symbol and input pointer is pointing to #.
Reject:Parsing is terminated because there are some errors in
the given sentence.
-
8/11/2019 COMPILER PREDICTIVE PARSER
50/145
Oxford University Press 2013. All rights reserved.
Simple stack based parser - Example
expr expr + term | termterm term * fact | factfact (expr) | id | cons
-
8/11/2019 COMPILER PREDICTIVE PARSER
51/145
Oxford University Press 2013. All rights reserved.
Simple stack based parserExample
..Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
52/145
Oxford University Press 2013. All rights reserved.
Conflicts in shift reduce parsing
Shift-reduce conflict:This conflict occurs when the parser has choice toselect both shift and reduce action
reduce-reduce conflict :
This conflict occurs when the top of the stack hashandle for which there is more than one reductionpossible
These conflicts are resolved in operator precedenceparser and LR parser
-
8/11/2019 COMPILER PREDICTIVE PARSER
53/145
Oxford University Press 2013. All rights reserved.
Operator Grammar and Parser
Operator grammar is a type of context free grammar,where there is no single production having adjacent non-terminals in the body of the production
Example:exp exp + exp | exp * exp | id
However, a grammar equivalent to this is:exp exp op exp | id
op + | *
It is not operator grammar
-
8/11/2019 COMPILER PREDICTIVE PARSER
54/145
Oxford University Press 2013. All rights reserved.
Operator Precedence Parsing
The precedence relations between two adjacent terminals aand b can be:
1. a has higher precedence than b i.e., a takes
precedence over b denoted as a
.
> b
2. a has lower precedence than b i.e., a gives precedenceto b denoted as a
-
8/11/2019 COMPILER PREDICTIVE PARSER
55/145
Oxford University Press 2013. All rights reserved.
The parsing steps are as follows:
1. Parser starts scanning from left to right till itencounters first .> relation and set a pointer
2. Scan backward till it encounters
-
8/11/2019 COMPILER PREDICTIVE PARSER
56/145
Oxford University Press 2013. All rights reserved.
AlgorithmOperatorParsing(w#)
//w the sentence to be parsed// ip input pointer// buffer[ip] next input symbol in buffer pointed by input
//pointer// M(a,b) Operator Precedence entry for the terminal as
//row and bs column// Anon-terminal in the head of production for the
handle //in the top of
// stack
// handle is popped from stack// returns 1, if parsing is completed successfully// -1, else
Al ith O t P i ( #) C td
-
8/11/2019 COMPILER PREDICTIVE PARSER
57/145
Oxford University Press 2013. All rights reserved.
ip=1
a = top(stack)b = buffer[ip]handle = emptyDo{
if(M(a,b) == )//handle is found on top of stack{
do{
AlgorithmOperatorParsing(w#) Contd
Al ith O t P i ( #) C td
-
8/11/2019 COMPILER PREDICTIVE PARSER
58/145
Oxford University Press 2013. All rights reserved.
do{
t = Pop(stack)handle =handle + t
//+ stands for concatenation}
while(t a)
c = top(stack)t = recently popped terminal
}while( M(c,t)
-
8/11/2019 COMPILER PREDICTIVE PARSER
59/145
Oxford University Press 2013. All rights reserved.
push(stack, A)//A is the head of the rule
//corresponding
//to the definition associated with the
//handle
}else
return -1}while(a # and b #)
return 1
AlgorithmOperatorParsing(w#) Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
60/145
Oxford University Press 2013. All rights reserved.
Operator Precedence Parsing-Example
Grammar:exp exp + exp | exp * exp | (exp) | id | cons
Precedence relation for an expression grammar
Parsing action using the operator
-
8/11/2019 COMPILER PREDICTIVE PARSER
61/145
Oxford University Press 2013. All rights reserved.
Parsing action using the operator
precedence table
-
8/11/2019 COMPILER PREDICTIVE PARSER
62/145
Oxford University Press 2013. All rights reserved.
Operator Precedence Table
Construction
The precedence and associativity rules are made use toconstruct the table
For example* .> +
Al i h P d T bl C (G P A)
-
8/11/2019 COMPILER PREDICTIVE PARSER
63/145
Oxford University Press 2013. All rights reserved.
AlgorithmPrecedenceTableConstruct(G,P,A)
// G the grammar used for precedence table
construction// P[a] precedence value of operator a given as ainteger, if //a .> b then// P[a] > P[b]
// A[a] Associativity value of operator and it is l for left//associative// and r for right associative// opd operand such as id, cons etc// returns M[a,b] which is the precedence table having//size n x n where n // is the number of terminals + 1
-
8/11/2019 COMPILER PREDICTIVE PARSER
64/145
Oxford University Press 2013. All rights reserved.
for each operator ain G do{
for each operator bin G do{
if(P[a] > P[b] )M[a,b] = .>M[b,a] =
-
8/11/2019 COMPILER PREDICTIVE PARSER
65/145
Oxford University Press 2013. All rights reserved.
{if(A[a] == l)
{M[a,b] = .>M[b,a] = .>
}else if(A[a] = = r){
M[a,b] =
-
8/11/2019 COMPILER PREDICTIVE PARSER
66/145
Oxford University Press 2013. All rights reserved.
M[a,opd] = M[a,#] = .>M[#,a] =
-
8/11/2019 COMPILER PREDICTIVE PARSER
67/145
Oxford University Press 2013. All rights reserved.
M[opd,#] = .>M[#, opd] = M[(, opd ] =
-
8/11/2019 COMPILER PREDICTIVE PARSER
68/145
Oxford University Press 2013. All rights reserved.
Construction of Precedence Table -
Example
Grammar:
exp exp + exp | exp * exp | (exp) | id | cons
Precedence and associativity of the operators are
shown as:
Operator Precedence Associativity
+ 1 L* 2 L
The constructed operator precedence table
-
8/11/2019 COMPILER PREDICTIVE PARSER
69/145
Oxford University Press 2013. All rights reserved.
The constructed operator precedence table
Limitations of operator precedence
-
8/11/2019 COMPILER PREDICTIVE PARSER
70/145
Oxford University Press 2013. All rights reserved.
Limitations of operator precedence
parser
1. This parser can be constructed only for operator grammar
2. If in a grammar, an operator has multiple precedence
values, then operator precedence parser cannot beconstructed.
For example the operator -can be either unaryminusorbinaryminuswhich differs in the precedence values.
-
8/11/2019 COMPILER PREDICTIVE PARSER
71/145
Oxford University Press 2013. All rights reserved.
Components of LR Parser
1. Input buffer which holds the sentence appended with #
2. Output buffer which tells the type of action selected
3. Stack used to hold the grammar symbols and theirstates during parsing
4. Parsing table which helps to select the parsing action
5. Parser routine which is the code for the parser
-
8/11/2019 COMPILER PREDICTIVE PARSER
72/145
Oxford University Press 2013. All rights reserved.
Types of LR Parser
1. Simple LR parser known as SLRparser
2. Canonical LR parser known as CLRparser
3. Look Ahead LR parser known as LALRparser
-
8/11/2019 COMPILER PREDICTIVE PARSER
73/145
Oxford University Press 2013. All rights reserved.
AlgorithmLR_Parsing(w#)
//w sentence to be parsed// ip input pointer// buffer[ip] next input symbol// s start state which is alone initially present in the stack// p,q,t states used in parsing process
// A(p,a)action part of the parsing table for the state p andthe //terminal a// G(p,A) goto part of the parsing table for the state p and
the non
//terminal A// returns 1, on accept// -1, on reject
Algorithm LR Parsing(w#) Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
74/145
Oxford University Press 2013. All rights reserved.
ip=1// Input pointer is initialized to point to left most token
push(stack,s)// store the start state alone in the stackp = top(stack)a = buffer[ip]while(1){
if(A(p,a) = (s,q) )// if the action to be selected is shift{
push(stack, a)push(stack, q)ip=ip+1
}else if (A(p,a) = (r, A) )// If the action is to reduce
AlgorithmLR_Parsing(w#) Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
75/145
LR Parsing Example
-
8/11/2019 COMPILER PREDICTIVE PARSER
76/145
Oxford University Press 2013. All rights reserved.
LR Parsing - Example
Grammar:expr expr + termexpr termterm term *factterm factfact (expr)fact id
String for Parsing:
id * id + id
LR Parsing Table
-
8/11/2019 COMPILER PREDICTIVE PARSER
77/145
Oxford University Press 2013. All rights reserved.
LR Parsing Table
Parsing action for the arithmetic expression
-
8/11/2019 COMPILER PREDICTIVE PARSER
78/145
Oxford University Press 2013. All rights reserved.
Parsing action for the arithmetic expression
id * id + idusing SLR parsing table
-
8/11/2019 COMPILER PREDICTIVE PARSER
79/145
Oxford University Press 2013. All rights reserved.
Parsing action for the arithmetic expression
id (id + id) using SLR parsing table
E E t i
-
8/11/2019 COMPILER PREDICTIVE PARSER
80/145
Oxford University Press 2013. All rights reserved.
Error Entries
1. If in state 0 and any operator is seen, the possibleerror message is: Expected Operand but operatorfound or missing operand
2. If in state 0, the close parenthesis is seen, actually it isa un-matching parenthesis. Hence the possible error
message is Unbalanced parenthesis3. If in state 1, id is seen the valid entries are only + and
hence the error message could be operator expectedbut operand found or missing operator
4. In a similar manner, if in state 0,4,6,7,8 the end ofmarker # is seen, the error message like unexpectedend of expression
SLR Parser
-
8/11/2019 COMPILER PREDICTIVE PARSER
81/145
Oxford University Press 2013. All rights reserved.
Viable PrefixesViable prefixes are the prefixes of right sententialforms. These prefixes are used for identifying the actionto be performed during the parsing process. Theinformation of viable prefixes is obtained using LR(0)
items.
LR(0) itemLR(0) item is a CFG of the form A having . in
the right side of the production rule.Example:
A. and A
Al i h f i LR P
-
8/11/2019 COMPILER PREDICTIVE PARSER
82/145
Oxford University Press 2013. All rights reserved.
Algorithm for constructing LR Parser
1. ClosureLR0(I)
2. GotoLR0(I, X)
3. ConstructLR0ItemsSet(G)
4. ConsturctSLRTable(G)
Al ith Cl LR0(I)
-
8/11/2019 COMPILER PREDICTIVE PARSER
83/145
Oxford University Press 2013. All rights reserved.
Algorithm ClosureLR0(I)
// I set of LR(0) items// a a single LR(0) item// returns J set of LR(0) items that can be reached
//without changing the
// parsers state
Algorithm ClosureLR0(I)Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
84/145
Oxford University Press 2013. All rights reserved.
for each LR(0) item a in I do{
J = J {a}push(stack, a)
}While(stack is not empty){
a = pop(stack)if( a == A.B)
for each production B do{
J = J { B .}
push (stack, B .)}
}return J
Algorithm ClosureLR0(I)Contd
ClosureLR0(I) - Example
-
8/11/2019 COMPILER PREDICTIVE PARSER
85/145
Oxford University Press 2013. All rights reserved.
ClosureLR0(I) - Example
expr expr + termexpr termterm term * fact
term factfact (expr)fact id
closure ({expr . expr + term })={expr . expr + term,expr . term,term . term * fact,term . fact,fact . (expr),fact . id
}
Given Grammar
Algorithm GotoLR0(I X)
-
8/11/2019 COMPILER PREDICTIVE PARSER
86/145
Oxford University Press 2013. All rights reserved.
AlgorithmGotoLR0(I, X)
// I set of LR(0) items// X grammar symbol in the given grammar
// a a single LR(0) item// returns K set of LR(0) items that can be reached
//after processing X{
for each LR(0) itemA.XdoJ = J { AX.}
K = Closure0(J)return K
}
GotoLR0(I X) Example
-
8/11/2019 COMPILER PREDICTIVE PARSER
87/145
Oxford University Press 2013. All rights reserved.
GotoLR0(I, X) - Example
goto({fact .(expr), ( })
goto({fact . (expr), ( }) = closure ({fact ( . expr) })=
{
fact ( . expr),expr . expr + termexpr . termterm . term * fact
term . Fact,fact . (expr), fact . id}
Algorithm ConstructLR0ItemsSet(G)
-
8/11/2019 COMPILER PREDICTIVE PARSER
88/145
Oxford University Press 2013. All rights reserved.
Algorithm ConstructLR0ItemsSet(G )
//Gthe grammar G including the augmented production S //S
//I, J, Kset of LR(0) items//a,b single LR(0) item//X grammar symbol of grammar G// returns C canonical collection of set of LR(0) items{
C = ClosureLR0({S.S})for each new LR(0) item I added to C do
for each grammar symbol X in G doif(GotoLR0(I,X) is not in C and
GotoLR0(I,x) )
C = C GotoLR0(I,X)return C}
AlgorithmConsturctSLRTable(G)
-
8/11/2019 COMPILER PREDICTIVE PARSER
89/145
Oxford University Press 2013. All rights reserved.
// G1the grammar G including the augmented production S //S// C canonical collection of set of LR(0) items// A(p,a) action part of parsing table for state p and terminal //a// G(p,A)goto part of parsing table for state p and non-//terminal A// returnsboth A and G
g ( )
AlgorithmConsturctSLRTable(G)Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
90/145
Oxford University Press 2013. All rights reserved.
C = ConstructLR0ItemsSet(G)
for each item I in C do{ p = state number representing Iq = state number representing Jif( A .ais in I and Goto0(I,a) = J)
A(p,a) = (s, q)else if (S S. is in I)
A(p,#) = accelse if(A . is in I)
for each b in follow(A) doA(p,b) = (r, A)
if( A .Bis in I and Goto0(I,B) = J)
G(p,B) = q}return A and G
ConstructLR0ItemsSet(G) - Example
-
8/11/2019 COMPILER PREDICTIVE PARSER
91/145
Oxford University Press 2013. All rights reserved.
( ) p
Grammar:
expr expr + termexpr termterm term * factterm fact
fact (expr)fact id
I0= closure ({expr
.expr}) ={expr .exprexpr .expr + term
expr .termterm .term * factterm .factfact .(expr)
fact .id}
I1 = goto(I0, expr)={expr expr.expr expr. +term}
ConstructLR0ItemsSet(G) Example Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
92/145
Oxford University Press 2013. All rights reserved.
I3= goto(I0, fact) ={term fact.}
I4= goto(I0, ( ) =
{fact ( .expr)expr .expr + termexpr .termterm .term * factterm .fact
fact . (expr)fact .id}
I5= goto(I0, id) =
{fact id.}
I6= goto(I1, +) ={expr expr + .termterm .term * fact
term .factfact . (expr)fact .id}
I7= goto(I2, *)={term term * .factfact .(expr)fact .id}
I2= goto(I0, term)={expr term.term term. * fact}
ExampleContd
ConstructLR0ItemsSet(G) Example Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
93/145
Oxford University Press 2013. All rights reserved.
I6= goto(I1, +)=
{expr expr + .termterm .term * factterm .factfact . (expr)fact .id
}
I7= goto(I2, *)=
{term term * .factfact .(expr)fact .id}
I8= goto(I4, expr)=
{fact ( expr .)expr expr. +term}
I9= goto(I6, term) ={expr expr + term.
term term. * fact}
I10= goto( I7, fact) ={term term * fact.
}
I11= goto(I8, ) ) ={fact ( expr) .
}
ExampleContd
Action and Goto fields in the LR
i bl
-
8/11/2019 COMPILER PREDICTIVE PARSER
94/145
Oxford University Press 2013. All rights reserved.
parsing table
Construct SLR Parsing TableExample
-
8/11/2019 COMPILER PREDICTIVE PARSER
95/145
Oxford University Press 2013. All rights reserved.
g p
(if-else statement)
Grammar:stat if cond then statstat if cond then stat else statstat othercondexpr
I0= closure ({stat .stat})={stat .statstat .if cond then statstat .if cond then stat else stat
stat .otherCond.expr}
I1 = goto(I0, stat) ={stat stat.}
Construct SLR Parsing Table
E l (if l t t t) C td
-
8/11/2019 COMPILER PREDICTIVE PARSER
96/145
Oxford University Press 2013. All rights reserved.
I2= goto(I0, if) ={stat if .cond then statstat if .cond then stat
else statcond.expr}
I3 = goto(I0, other) ={stat other.}
I4 = goto(I0, expr) ={
condexpr.}
I5= goto(I2, cond) ={stat if cond .then stat
statif cond .then stat else stat}
Example (if-else statement) Contd
Construct SLR Parsing TableExample(if-else statement) Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
97/145
Oxford University Press 2013. All rights reserved.
I6= goto(I5, then) =
{stat if cond then .statstat if cond then .stat else statstat .if cond then statstat .if cond then stat else statstat .othercond.expr}
I7= goto(I6, stat) ={Statif cond then stat .stat if cond then stat .else stat}
I8= goto(I7, else) ={stat if cond then stat else.statstat .if cond then statstat.if cond then stat elsestat
stat .othercond.expr}
I9= goto(I8, stat) =
{stat if cond then stat elsestat.}
(if-else statement) Contd
Construction of Action and Goto fields forSLR Parsing Table Example (if-else
-
8/11/2019 COMPILER PREDICTIVE PARSER
98/145
Oxford University Press 2013. All rights reserved.
SLR Parsing TableExample (if-else
statement)
SLR parsing table with shift reduce
fli t
-
8/11/2019 COMPILER PREDICTIVE PARSER
99/145
Oxford University Press 2013. All rights reserved.
conflicts
SLR parsing table construction fora function call grammar
-
8/11/2019 COMPILER PREDICTIVE PARSER
100/145
Oxford University Press 2013. All rights reserved.
GrammarSid ( P )SEPidEid ( E )Eid
a function call grammar
I0= {S1.SS.id (P)S.EE.id (E)
E
.id}
Goto (I0, S) = I1={S1S.}
Goto (I0, E) = I2={SE.}
Goto (I0, id)=I3={Sid.( P )
Eid.(E)Eid.}
Goto (I3, ( ) =I4={Sid(.P)P.id
Eid(.E)E.id(E)E.id}
Goto (I4,P)=I5={Sid(P.)}
SLR parsing table construction fora function call grammar contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
101/145
Oxford University Press 2013. All rights reserved.
Goto (I4,E)=I6=
{Eid(E.)}
Goto (I4, id)=I7={
Pid.Eid.(E)Eid.}
Goto (I5,) ) = I8 ={Sid(P).}
Goto (I6, ) )=I9=
{Eid(E).}
Goto(I7, ( ) = I10=
{Eid(.E)E.id ( E )E.id}
Goto(I10, E)=I11={Eid(E.)}
Goto(I10,id)=I12=
{Eid.(E)Eid.}
Goto(I11, ) )= I13={Eid(E).}
Goto(I12, ( )=I10
a function call grammarcontd
-
8/11/2019 COMPILER PREDICTIVE PARSER
102/145
SLR parsing table for a function call
-
8/11/2019 COMPILER PREDICTIVE PARSER
103/145
Oxford University Press 2013. All rights reserved.
CLR Parser LR(1) Parser
-
8/11/2019 COMPILER PREDICTIVE PARSER
104/145
Oxford University Press 2013. All rights reserved.
CLR Parser LR(1) Parser
Algorithms:ClosureLR1(I)
GotoLR1(I, X)
ConstructLR1ItemsSet(G)
ConsturctCLRTable(G)
AlgorithmClosureLR1(I)
-
8/11/2019 COMPILER PREDICTIVE PARSER
105/145
Oxford University Press 2013. All rights reserved.
// I set of LR(1) items// a a single LR(1) item//b, c a single terminal// returns J set of LR(1) items that can be reached
without //changing the
// parsers state
AlgorithmClosureLR1(I) Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
106/145
Oxford University Press 2013. All rights reserved.
for each LR(1) item a in I do{
J = J {a}push(stack, a)
}While(stack is not empty){
if( a == [A .B,b])
for each production B dofor each terminal c in first(b)do{
J = J { [B .,c] }push (stack, [B .,c])
}}return J
Algorithm GotoLR1(I, X)
-
8/11/2019 COMPILER PREDICTIVE PARSER
107/145
Oxford University Press 2013. All rights reserved.
Algorithm GotoLR1(I, X)
// I set of LR(1) items// X grammar symbol in the given grammar
// returns K set of LR(1) items that can be reached
//after processing X
{for each LR(1) item [A .X, b] do
J = J { [A X., b]}K = ClosureLR1(J)
return K}
Algorithm
C t tLR1It S t(G)
-
8/11/2019 COMPILER PREDICTIVE PARSER
108/145
Oxford University Press 2013. All rights reserved.
ConstructLR1ItemsSet(G)
//G the grammar G including the augmented production //S S// I, J, K set of LR(1) items//a,b single LR(1) items// c,d single terminal// returns C canonical collection of set of LR(1) items{
C = Closure1({[S .S, #]})for each new LR(1) item I added to C do
for each grammar symbol X in G doif( Goto1(I,X) is not in C and Goto1(I,x) )
C = C Goto1(I,X)
return C}
AlgorithmConstructCLRTable(G)
-
8/11/2019 COMPILER PREDICTIVE PARSER
109/145
Oxford University Press 2013. All rights reserved.
// Gthe grammar G including the augmentedproduction //SS// C canonical collection of set of LR(0) items// A(p,a) action part of parsing table for state p and
//terminal a// G(p,A) goto part of parsing table for state p and non-
//terminal A// returns both A and G
AlgorithmConstructCLRTable(G) Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
110/145
Oxford University Press 2013. All rights reserved.
C = ConstructLR1ItemsSet(G)for each item I in C do{
p = state number representing Iq = state number representing Jif( [A.a,b] is in I and Goto1(I,a) = J)
A(p,a) = (s, q)else if ([S S.,#] is in I)
A(p,#) = accelse if([A ., b] is in I)
A(p,b) = (r, A)
if( [A.B,b] is in I and Goto1(I,B) = J)G(p,B) = q
}return A and G
ConstructCLRTable(G )Contd
CLR Parser Construction - Example
-
8/11/2019 COMPILER PREDICTIVE PARSER
111/145
Oxford University Press 2013. All rights reserved.
CLR Parser Construction Example
Grammar:Stat LHS = RHSStat RHSLHS *RHS
LHS idRHS LHS
I0= closure ({[Stat. Stat, #] })={[Stat . Stat, #][Stat . LHS = RHS, #][Stat . RHS, #][LHS . *RHS, =][LHS . id, =][RHS . LHS, #][LHS . *RHS, #][LHS . id, #]
}
I1= goto(I0, Stat) ={
[Stat Stat . , #]}
CLR Parser Construction
E l C d
-
8/11/2019 COMPILER PREDICTIVE PARSER
112/145
Oxford University Press 2013. All rights reserved.
I2= goto(I0,LHS) =
{[Stat LHS . = RHS, #][RHS LHS . , #]}
I3= goto(I0,RHS) ={[Stat RHS . , #]}
I4= goto(I0,*) ={[LHS * . RHS, =][LHS * . RHS, #][RHS . LHS, =]
[RHS . LHS, #][LHS . *RHS, =][LHS . id, =][LHS . *RHS, #][LHS . id, #]}
I5= goto(I0,id) =
{[LHS id., =][LHS id . , #]}
I6=goto(I2,=) ={[Stat LHS = .RHS, #]
[RHS . LHS, #][LHS . *RHS, #][LHS . id, #]}
ExampleContd
CLR Parser Construction
Example Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
113/145
Oxford University Press 2013. All rights reserved.
I7= goto(I4, LHS) =
{[RHS LHS . , =][RHS LHS . , #]}
I8= goto(I4, RHS) ={[LHS * RHS . , =][LHS * RHS . , #]}
goto(I4,*) = I4goto(I4,id) = I5
I9= goto(I6, LHS) =
{[RHS LHS ., #]}
I10= goto(I6, RHS)
={[ Stat LHS =RHS . , #]}
I11= goto(I6,*) =
{[LHS * . RHS, #][RHS . LHS, #][LHS . *RHS, #][LHS . id, #]
}
I12= goto(I6,id) ={[LHS id ., #]}
goto(I11,LHS) = I9I13=goto(I11,RHS) ={[LHS * RHS . , #]}
goto(I11,*) = I11goto(I11,id) = I12
ExampleContd
Kernel Items
-
8/11/2019 COMPILER PREDICTIVE PARSER
114/145
Oxford University Press 2013. All rights reserved.
I0= { [Stat . Stat, #]},
I1= {[Stat Stat . , #] }I2= {[Stat LHS . = RHS, #], [RHS LHS . , #] }I3= {[Stat RHS . , #] }I4= {[LHS * . RHS, =], [LHS * . RHS, #]}I5= {[LHS id., =], [LHS id . , #] }
I6={[Stat LHS = . RHS, #]}I7= {[RHS LHS . , =], [RHS LHS . , #] }I8= {[LHS * RHS . , =], [LHS * RHS . , #]}I9= { [RHS LHS ., #] }I10= {[ Stat LHS = RHS . , #]}I11= {[LHS * . RHS, #]}I12= {[LHS id ., #]}I13= {[LHS * RHS . , #] }
CLR Parsing Table
-
8/11/2019 COMPILER PREDICTIVE PARSER
115/145
Oxford University Press 2013. All rights reserved.
LALR Parser
-
8/11/2019 COMPILER PREDICTIVE PARSER
116/145
Oxford University Press 2013. All rights reserved.
I4={[LHS * . RHS, =][LHS * . RHS, #][RHS . LHS, =][RHS . LHS, #][LHS . *RHS, =][LHS . id, =][LHS . *RHS, #][LHS . id, #]}
I11= {[LHS * . RHS, #][RHS . LHS, #][LHS . *RHS, #][LHS . id, #]
}
I4and I11have the similar corecomponents
(I4, I11), (I5, I12), (I7, I9),(I8, I13)are equivalentitems
LALR parsing table
-
8/11/2019 COMPILER PREDICTIVE PARSER
117/145
Oxford University Press 2013. All rights reserved.
p g
Design of Data Structure -
-
8/11/2019 COMPILER PREDICTIVE PARSER
118/145
Oxford University Press 2013. All rights reserved.
Predictive Parser
typedef char* FIRST;typedef char* FOLLOW;
typedef struct FIRST
{char* symName;FIRST* values;
}first;
-
8/11/2019 COMPILER PREDICTIVE PARSER
119/145
Oxford University Press 2013. All rights reserved.
typedef struct FOLLOW
{char* ntName; //Non-terminalFOLLOW* values;
}follow;
typedef struct Production{
char* symName;struct Production * next;
}Production;
Production * PredictTable[NTSIZE][TSIZE+1];
-
8/11/2019 COMPILER PREDICTIVE PARSER
120/145
Design of Data Structure - SLR Parser
-
8/11/2019 COMPILER PREDICTIVE PARSER
121/145
Oxford University Press 2013. All rights reserved.
typedef struct ProductionWithDotBody{
char* symName;struct ProductionWithDotBody * next;
}ProductionWithDotBody;
typedef struct ProductionWithDotHead
{char* symName;struct ProductionWithDotHead * nextHead;struct ProductionWithDotBody * next;
}ProductionWithDotHead;
Ttypedef ProductionWithDotHead LR0Items;typedef LR0Items * LR0ItemsCollection;
Data Structure for LR(0) items:
-
8/11/2019 COMPILER PREDICTIVE PARSER
122/145
Oxford University Press 2013. All rights reserved.
( )
{A .B c, B .eD }
Data Structure for Action Field
-
8/11/2019 COMPILER PREDICTIVE PARSER
123/145
Oxford University Press 2013. All rights reserved.
typedef structActionNode{char action;// s shift, r reduce ,// a accept, e error
short num;// represents state number or rule numberstruct ActionNode * next;// provision for multiple entries
}ActionNode;
Data Structure for Action Field
Data Structure for Goto Field
-
8/11/2019 COMPILER PREDICTIVE PARSER
124/145
Oxford University Press 2013. All rights reserved.
typedef struct GotoNode{
short state;struct GotoNode * next;
}GotoNode;
ActionNode SLRTableAction[STATES][TSIZE+1];
GotoNode SLRTableGoto[STATES][NTSIZE];
Data Structure for Goto Field
Usage of YACC for Parser
G t
-
8/11/2019 COMPILER PREDICTIVE PARSER
125/145
Oxford University Press 2013. All rights reserved.
YACC
Syntactic
specifications Parser as Croutine
Generator
Structure of YACC specification
-
8/11/2019 COMPILER PREDICTIVE PARSER
126/145
Oxford University Press 2013. All rights reserved.
Definition Section
%%
Rules Section
%%
To be included only if user sub section is present
User Subroutine Section
$ yacc a.y
YACC and Lex put together
-
8/11/2019 COMPILER PREDICTIVE PARSER
127/145
Oxford University Press 2013. All rights reserved.
p g
cc y.tab.c lex.yy.c -llly
Lexical Specifications for Calculator
Program
-
8/11/2019 COMPILER PREDICTIVE PARSER
128/145
Oxford University Press 2013. All rights reserved.
Program
%{#include#include
#include"y.tab.h"%}%%
[0-9]+ {ylval.i= atoi(yytext);flag=1;return INTEGER;
}
Grammar:
S EE E + E | E *E | (E) | const
Lexical Specifications for Calculator
Program Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
129/145
Oxford University Press 2013. All rights reserved.
[0-9]+[.][0-9]+ { yylval.dval=atof(yytext);flag=1;return REAL;
}
+" { flag=0;return PLUS;
}*" { flag=0;
return MUL;}
"/" { flag=0;return DIV;
}
ProgramContd
Lexical Specifications for Calculator
ProgramContd
-
8/11/2019 COMPILER PREDICTIVE PARSER
130/145
Oxford University Press 2013. All rights reserved.
"(" { flag=0;return OPENB;
}
")" { flag=1;return CLOSEB;}
[\n] {return NEWLINE;
}%%
ProgramContd
Syntactic Specifications for Calculator
Program
-
8/11/2019 COMPILER PREDICTIVE PARSER
131/145
Oxford University Press 2013. All rights reserved.
Program
%{#include#include%}
%union{
int i;float dval;
}
Syntactic Specifications for Calculator
Program Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
132/145
Oxford University Press 2013. All rights reserved.
%token OPENB CLOSEB NEWLINE%token INTEGER%token REAL
%left PLUS%left MUL DIV%type exp
ProgramContd
Syntactic Specifications for Calculator
Program Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
133/145
Oxford University Press 2013. All rights reserved.
%%
beg : exp NEWLINE{
printf("The value of the expression is%f\n",$1);
exit(0);
} ;exp : exp PLUS exp{
$$=$1+$3;}| exp MUL exp{
$$=$1*$3;}
ProgramContd
-
8/11/2019 COMPILER PREDICTIVE PARSER
134/145
Lexical Specifications for Calculator
Program with grammar rewritten
-
8/11/2019 COMPILER PREDICTIVE PARSER
135/145
Oxford University Press 2013. All rights reserved.
Program with grammar rewritten
Grammar:S - > EE -> E+T | TT -> T *F | T /F | FF-> (E) | const
%{extern int yylval;
%}%%"+" { return plus;}
"*" { return mul;}"\n" { return newline;}"(" { return openp;}")" { return closep;}
Lexical Specifications for Calculator
Program with grammar
-
8/11/2019 COMPILER PREDICTIVE PARSER
136/145
Oxford University Press 2013. All rights reserved.
[0-9]+ {yylval=atoi(yytext);return number;
}%%yywrap(){
printf("eof reached\n");return 1;
}
Program with grammar
rewrittenContd
Syntax Specifications for Calculator
Program with grammar rewritten
-
8/11/2019 COMPILER PREDICTIVE PARSER
137/145
Oxford University Press 2013. All rights reserved.
%{#include%}%token plus mul newline%token number%%lines : lines line | line
;line : E newline {printf("%d\n",$1);}
;E : E plus T {$$ = $1 + $3;}
| T {$$ = $1;};
g g
-
8/11/2019 COMPILER PREDICTIVE PARSER
138/145
-
8/11/2019 COMPILER PREDICTIVE PARSER
139/145
Infix to Postfix Expression
Syntax Specification
-
8/11/2019 COMPILER PREDICTIVE PARSER
140/145
Oxford University Press 2013. All rights reserved.
assig : ID EQUAL exp NEWLINE{
strcpy($$,"");strcat($$,$1);strcat($$,$3);
strcat($$,"=");printf("The postfix expression is
%s\n,$$);exit(0);
}
Syntax Specification
Infix to Postfix Expression
Syntax Specification Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
141/145
Oxford University Press 2013. All rights reserved.
exp : exp PLUS exp{ strcpy($$,""); strcat($$,$1);
strcat($$,$3); strcat($$,"+");}| exp MUL exp{
strcpy($$,""); strcat($$,$1);strcat($$,$3); strcat($$,"*");
}|exp BMINUS exp{
strcpy($$,""); strcat($$,$1);strcat($$,$3); strcat($$,"-");
}
y p
Infix to Postfix Expression
Syntax Specification Contd
-
8/11/2019 COMPILER PREDICTIVE PARSER
142/145
Oxford University Press 2013. All rights reserved.
| UMINUS exp
{ strcpy($$,""); strcat($$,$2);strcat($$,"-");
}| OPENB exp CLOSEB{
strcpy($$,""); strcat($$,$2);}| ID{
strcpy($$,""); strcat($$,$1);
} ;
Infix to Postfix Expression
Calling and error handling
-
8/11/2019 COMPILER PREDICTIVE PARSER
143/145
Oxford University Press 2013. All rights reserved.
int main(){
yyparse();return 0;
}
yyerror(char *msg){
fprintf(stderr,"%s\n",msg);}
g g
Error Handling
-
8/11/2019 COMPILER PREDICTIVE PARSER
144/145
Oxford University Press 2013. All rights reserved.
Categories of Error
Error Location
Error Recovery
Error Reporting
KEY TERMS
-
8/11/2019 COMPILER PREDICTIVE PARSER
145/145
Role of Parser Grammar and languages Ambiguous Grammar and its elimination Parser and its Types Top-down parserrecursive descent and predictive
parser Bottom-up ParserStack based parser, operator
precedence parser, LR Parser (SLR, CLR, LALR) Parsing tools (YACC)
Implementation techniques Error Handling