0% found this document useful (0 votes)
42 views22 pages

Lec 05

Uploaded by

Lilian Voss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views22 pages

Lec 05

Uploaded by

Lilian Voss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture 05

Syntax Analysis in Compiler


Topics

• Syntax Analysis
• Recursive-Descent Parsing
• Bottom-Up Parsing

Copyright © 2015 Pearson. All rights reserved. 1-2


Syntax Analysis

• Process analyzing syntax is referred as


syntax analysis or often called parsing.
• Goals of the parser, given an input
program:
– Find all syntax errors; for each, produce an
appropriate diagnostic message and recover
quickly
– Produce the parse tree, or at least a trace of the
parse tree, for the program

Copyright © 2015 Pearson. All rights reserved. 1-3


The Parsing Problem

• Two categories of parsers


– Top down - produce the parse tree, beginning
at the root
• Order is that of a leftmost derivation
• Traces or builds the parse tree in preorder

– Bottom up - produce the parse tree, beginning


at the leaves
• Order is that of the reverse of a rightmost derivation
• Useful parsers look only one token ahead in
the input

Copyright © 2015 Pearson. All rights reserved. 1-4


The Parsing Problem (continued)

• Top-down Parsers
– Given a sentential form, xA , the parser must
choose the correct A-rule to get the next
sentential form in the leftmost derivation, using
only the first token produced by A

• The most common top-down parsing algorithms:


– Recursive descent (based on EBNF)
– LL parsers (left-to-right scan of input with
leftmost derivation)

Copyright © 2015 Pearson. All rights reserved. 1-5


The Parsing Problem (continued)

• Bottom-up parsers
– Given a right sentential form, , determine what substring
of  is the right-hand side of the rule in the grammar that
must be reduced to produce the previous sentential form
in the right derivation
– The most common bottom-up parsing algorithms are in
the LR family
<program> => <stmts> => <stmt>
=> <var> = <expr>
=> a = <expr>
=> a = <term> + <term>
=> a = <var> + <term>
=> a = b + <term>
=> a = b + const
Copyright © 2015 Pearson. All rights reserved. 1-6
Recursive-Descent Parsing

• There is a subprogram for each nonterminal in the


grammar, which can parse sentences that can be
generated by that nonterminal

• EBNF is ideally suited for being the basis for a


recursive-descent parser, because EBNF
minimizes the number of nonterminals

• A grammar for simple expressions:


<expr>  <term> {(+ | -) <term>}
<term>  <factor> {(* | /) <factor>}
<factor>  id | int_constant | ( <expr> )

Copyright © 2015 Pearson. All rights reserved. 1-7


Recursive-Descent Parsing (continued)

• Assume we have a lexical analyzer named lex,


which puts the next token code in nextToken
• The coding process when there is only one RHS:
– For each terminal symbol in the RHS, compare it
with the next input token; if they match,
continue, else there is an error
– For each nonterminal symbol in the RHS, call its
associated parsing subprogram

• This particular routine does not detect errors


• Convention: Every parsing routine leaves the next
token in nextToken
Copyright © 2015 Pearson. All rights reserved. 1-8
Recursive-Descent Parsing (continued)

• A nonterminal that has more than one RHS


requires an initial process to determine
which RHS it is to parse
– The correct RHS is chosen on the basis of the
next token of input (the lookahead)
– The next token is compared with the first token
that can be generated by each RHS until a match
is found
– If no match is found, it is a syntax error

Copyright © 2015 Pearson. All rights reserved. 1-9


Recursive-Descent Parsing (continued)

/* Function expr /* term


Parses strings in the language Parses strings in the language generated by the rule:
generated by the rule:
<expr> → <term> {(+ | -) <term>} <term> -> <factor> {(* | /) <factor>)
*/ */
void term() {
void expr() {

/* Parse the first term */ /* Parse the first factor */


factor();
term();
/* As long as the next token is + or -,
Call lex to get the next token and /* As long as the next token is * or /,
parse the next term */ next token and parse the next factor */
while (nextToken == MULT_OP || nextToken == DIV_OP)
while (nextToken == ADD_OP || {
nextToken == SUB_OP){ lex();
lex(); factor();
term(); }
} } /* End of function term */
}

Copyright © 2015 Pearson. All rights reserved. 1-10


Recursive-Descent Parsing (continued)

/* Function factor
Parses strings in the language
generated by the rule:
<factor> -> id | (<expr>) */

void factor() {

/* Determine which RHS */


if (nextToken) == ID_CODE || nextToken == INT_CODE)

/* For the RHS id, just call lex */


lex();

/* If the RHS is (<expr>) – call lex to pass over the


left parenthesis, call expr, and check for the right
parenthesis */
else if (nextToken == LP_CODE) {
lex();
expr();
if (nextToken == RP_CODE)
lex();
else
error();
} /* End of else if (nextToken == ... */

else error(); /* Neither RHS matches */


}

Copyright © 2015 Pearson. All rights reserved. 1-11


Recursive-Descent Parsing (continued)

- Trace of the lexical and syntax analyzers on (sum + 47) / total

Next token is: 25 Next lexeme is ( Next token is: 11 Next lexeme is total
Enter <expr> Enter <factor>
Enter <term> Next token is: -1 Next lexeme is EOF
Enter <factor> Exit <factor>
Next token is: 11 Next lexeme is sum Exit <term>
Enter <expr> Exit <expr>
Enter <term>
Enter <factor>
Next token is: 21 Next lexeme is +
Exit <factor>
Exit <term>
Next token is: 10 Next lexeme is 47
Enter <term>
Enter <factor>
Next token is: 26 Next lexeme is )
Exit <factor>
Exit <term>
Exit <expr>
Next token is: 24 Next lexeme is /
Exit <factor>

Copyright © 2015 Pearson. All rights reserved. 1-12


Recursive-Descent Parsing (continued)

– The Left Recursion Problem


• If a grammar has left recursion, either direct or
indirect, it cannot be the basis for a top-down parser
Left recursive : A → Aα1 | … | Aαm | β1 | β2 | … | βn

– A grammar can be modified to remove direct left recursion


as follows:
For each nonterminal, A,
1. Group the A-rules as A → Aα1 | … | Aαm | β1 | β2 | … | βn
where none of the β‘s begins with A
2. Replace the original A-rules with
A → β1A’ | β2A’ | … | βnA’
A’ → α1A’ | α2A’ | … | αmA’ | ε

Copyright © 2015 Pearson. All rights reserved. 1-13


Recursive-Descent Parsing (continued)

Replace E E + T
– X  X1 | ... | Xm E T E E + T | T
| 1 | ... | n T T * F T T * F | F
T F F  ( E ) | id
F ( E )
F  id

With E  T E'
– X  1X’ | ... | nX’ E'  + T E' | 
– X’  1X’ | ... | mX’ |  T  F T'
T'  * F T' | 
F  ( E ) | id
Copyright © 2015 Pearson. All rights reserved. 1-14
Bottom-up Parsing

• Intuition about handles (continued):


– The handle of a right sentential form is its
leftmost simple phrase
– Given a parse tree, it is now easy to find the
handle
– Parsing can be thought of as handle pruning

Copyright © 2015 Pearson. All rights reserved. 1-15


Bottom-up Parsing (continued)
• The parsing problem is finding the correct RHS in a
right-sentential form to reduce to get the previous
right-sentential form in the derivation
• The following rightmost derivation:

E E + T | T
T T * F | F
F  ( E ) | id

handle pruning

• The underlined part of each sentential form is


replaced with corresponding LHS
Copyright © 2015 Pearson. All rights reserved. 1-16
Bottom-up Parsing (continued)

• Shift-Reduce Algorithms

– Reduce is the action of replacing the handle on


the top of the parse stack with its
corresponding LHS

– Shift is the action of moving the next token to


the top of the parse stack

Copyright © 2015 Pearson. All rights reserved. 1-17


Bottom-up Parsing (continued)

• Advantages of LR parsers ("L" for left-to-right


scanning of the input, "R" for constructing a rightmost
derivation in reverse)
– They will work for nearly all grammars that
describe programming languages.

– They work on a larger class of grammars than


other bottom-up algorithms, but are as efficient
as any other bottom-up parser.

– They can detect syntax errors as soon as it is


possible.
Copyright © 2015 Pearson. All rights reserved. 1-18
Structure of An LR Parser

Copyright © 2015 Pearson. All rights reserved. 1-19


Bottom-up Parsing (continued)

• Initial configuration: (S0, a1…an$)


• Parser actions:
– For a Shift, the next symbol of input is pushed onto the
stack, along with the state symbol that is part of the Shift
specification in the Action table

– For a Reduce, remove the handle from the stack, along with
its state symbols. Push the LHS of the rule. Push the state
symbol from the GOTO table, using the state symbol just
below the new LHS in the stack and the LHS of the new rule
as the row and column into the GOTO table

– For an Accept, the parse is complete and no errors were


found.
– For an Error, the parser calls an error-handling routine.
Copyright © 2015 Pearson. All rights reserved. 1-20
Bottom-up Parsing (continued)
E E + T | T
T T * F | F
F  ( E ) | id

Copyright © 2015 Pearson. All rights reserved. 1-21


Bottom-up Parsing (continued)

Copyright © 2015 Pearson. All rights reserved. 1-22

You might also like