- Type
- Quiz
- Course
- CS606 Compiler Construction
- Size
- PDF 35 KB
- Updated
- 23 Oct 2026
You get 3 free files a day on the website. Add the Chrome extensionSign in with the extension for 30 a day.
Preview text
Text from pages 1, 2, 3 of 4. Download the file for all 4 pages.
Lecture 14
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' - term expr'
ε
6 term factor term' 7 term' * factor term' / factor term'
ε
10 factor number id ( 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(); Functions for other non-terminals Term, Factor and Tprime follow the same pattern.
Recursive Descent in C++
This form of the routines is too procedural. Moreover, there is no convenient way to build the parse tree. We can use C++ to code the recursive descent parser in an object oriented manner. We associate a C++ class with each non-terminal symbol. An instantiated object of a non-terminal class contains pointer to the parse tree. Here are the C++ code for the non-terminal classes:
Related CS606 files
All CS606 files →Get more out of VUBuddy, for free
- Unlimited downloads
- The explanation behind every MCQ answer
- Study offline on slow data
- 30 downloads a day instead of 3
- Sign in and buy Pro Semester here
One Pro Semester membership works in both the extension and the app.