


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: Ll, Property, Follow, If, Code, Predictive, Grammers, Recursive, Descent, parsing
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



The LL(1) Property
If A? α and A? β both appear in the grammar, we would like
FIRST( α ) ∩ FIRST( β ) = ∅
Predictive parsers accept LL(k) grammars. The two LL stand for Left-to-right scan of input, left- most derivation. The k stands for number of look-ahead tokens of input. The LL(1) Property allows the parser to make a correct choice with a look-ahead of exactly one symbol! What about ε-productions? They complicate the definition of LL(1). If A? α and A? β and ε ∈ FIRST( α ) , then we need to ensure that FIRST( β ) is disjoint from FOLLOW( α ), too.
Definition: FOLLOW( α ) is the set of all words in the grammar that can legally appear after an α.
For a non-terminal X , FOLLOW( X ) is the set of symbols that might follow the derivation of X. Define FIRST+( α ) as FIRST( α ) ∪ FOLLOW( α ), if ε ∈ FIRST( α ), FIRST( α ), otherwise. Then a grammar is LL(1) iff A? α and A? β implies
FIRST+( α ) ∩ FIRST+( β ) = ∅
Given a grammar that has the is LL(1) property, we can write a simple routine to recognize each lhs. The code is simple and fast. Consider A? β 1 | β 2 | β 3 , which satisfies the LL(1) property FIRST+( α )∩FIRST+( β ) = ∅
/* find an A */ if(token ∈ FIRST( β 1 )) find a β 1 and return true else if(token ∈ FIRST( β 2 )) find a β 2 and return true if(token ∈ FIRST( β 3 )) find a β 3 and return true else error and return false
Grammar with the LL(1) property are called predictive grammars because the parser can “predict” the correct expansion at each point in the parse. Parsers that capitalize on the LL(1) property are called predictive parsers. One kind of predictive parser is the recursive descent parser.
Recursive Descent Parsing
Consider the right-recursive expression grammar
1 Goal? expr 2 expr? term expr' 3 expr'? + term expr' 4 | - term expr' 5 | (^) ε 6 term? factor term' 7 term'? * factor term' 8 | / factor term' 9 | (^) ε 10 factor? number 11 | id 12 | ( expr )
This leads to a parser with six mutually recursive routines: goal, expr, eprime, term, tprime and factor. Each recognizes one non-terminal (NT) or terminal (T). The term descent refers to the direction in which the parse tree is built. Here are some of these routines written as functions:
Goal() { token = next_token(); if(Expr() == true && token == EOF) next compilation step else { report syntax error; return false; } } Expr() { if(Term() == false) return false; else return Eprime(); } Eprime() { token_type op = next_token(); if( op == PLUS || op == MINUS ) { if(Term() == false) return false; else return Eprime(); }
class Term:public NonTerminal { public: Term(Scanner sc): NonTerminal(sc){ } virtual bool isPresent(); }*
class Tprime:public NonTerminal { public: Tprime(Scanner sc, TreeNode t): NonTerminal(sc) { exprSofar = t; } virtual bool isPresent(); protected: TreeNode* exprSofar; }**
class Factor:public NonTerminal { public: Factor(Scanner sc, TreeNode t): NonTerminal(sc){ };**
virtual bool isPresent();