


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Compile is software which translates high level programming language to computer basic language. Many compilers exist e.g. Borland, Java etc. Compiler construction have few steps which are taught in this course. This lecture includes: Parser, Algorithm, key, Recursion, Expr, Term, Predictive, Transformations, Sentential
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



A top-down parser starts with the root of the parse tree. The root node is labeled with the goal (start) symbol of the grammar. The top-down parsing algorithm proceeds as follows:
The key is picking right production in step a. That choice should be guided by the input string. Letโs try parsing using this algorithm using the expression grammar.
x โ 2 (^) * y
P Sentential Form input
This worked well except that โโโ does not match โ+โ. The parser made the wrong choice of production to use at step 2. The parser must backtrack and use a different production.
This time the โโโ and โโโ matched. We can advance past โ โ โ to look at โ 2 โ. Now, we need to expand โ term โ
P Sentential Form input
The 2โs match but the expansion terminated too soon because there is still unconsumed input and there are no non-terminals to expand in the sentential form โ Need to backtrack.
P Sentential Form input
This time the parser met with success. All of the input matched.
Left Recursion
Consider another possible parse:
Parser is using productions but no input is being consumed.
9 <id,x> โ <num,2> x โ โ (^2) * y
7 <id,x> โ factor x โ โ (^2) * y
P Sentential Form input
2 expr + term + term + term +....^ โ x^ โ^2 * y
2 expr + term + term + term^ โ x^ โ^2 * y
2 expr + term + term^ โ x^ โ^2 * y
expr + term
expr
Goal
Sentential Form
2^ โ x^ โ^2 * y
1^ โ x^ โ^2 * y
P input
Predictive Parsing
If a top down parser picks the wrong production, it may need to backtrack. Alternative is to look-ahead in input and use context to pick the production to use correctly. How much look-ahead is needed? In general, an arbitrarily large amount of look-ahead symbols are required.. Fortunately, large classes of CFGs can be parsed with limited lookahead. Most programming languages constructs fall in those subclasses
The basic idea in predictive parsing is: given A? ฮฑ | ฮฒ , the parser should be able to choose between ฮฑ and ฮฒ. To accomplish this, the parser needs FIRST and FOLLOW sets.
Definition: FIRST sets: for some rhs ฮฑ โ G , define FIRST( ฮฑ ) as the set of tokens that appear as the first symbol in some string that derives from ฮฑ. That is, x โ FIRST( ฮฑ ) iff ฮฑ โโ^ x ฮณ, for some ฮณ.