Professional Documents
Culture Documents
Introduction
Language processors (tombstone diagrams, bootstrapping)
Architecture of a compiler
Syntax analysis
Contextual analysis
Runtime organization
Code generation
Interpretation
Review
Nullable(t) = false
Repeat
For each production N ::= x1 x2 x3 xn
If Nullable(xi) for all of xi then set Nullable(N) to true
Repeat
For each production N ::= x1 x2 x3 xn
First(N) = First(N) First(x1)
For each i from 2 through n
If Nullable(x1 xi-1), then
First(N) = First(N) First(xi)
Follow(S) = {$}
// the end-of-file symbol
For each non-terminal N other than S
Follow(N) = { }
Repeat
For each production N ::= x1 x2 x3 xn
For each i from 1 through n-1
if xi is a non-terminal then
Follow(xi) = Follow(xi) First(xi+1 xn)
For each i from n downto 1
if xi is a non-terminal and Nullable(xi+1 xn) then
Follow(xi) = Follow(xi) Follow(N)
Nullable?
false
First
{a, e, d, c, f}
Follow
{$}
false
{a, e}
{f, $}
false
{f}
{c, d, $, a, e, b}
true
{c} or {c, }
{d, $, f}
true
{d} or {d, }
{$, c, f, d}
Parsing
Recognition
To answer the question does the input conform to the syntax of
the language
Some terminology
Different types of parsing strategies
bottom up
top down
Recursive descent parsing
What is it
How to implement a parser given an EBNF specification
Parsing
Recognition + also determine structure of program (for example
by creating an AST data structure)
Unambiguous grammar:
A grammar is unambiguous if there is only at most one way to
parse any input. (i.e. for syntactically correct program there is
precisely one parse tree)
10
Bottom up parsing
The parse tree grows from the bottom (leafs) up to the top (root).
bottom up strategies
top down strategies
::=
::=
::=
::=
::=
::=
::=
::=
::=
::=
Sentence
Subject
Subject Verb
Verb Object
Object ..
II || AA Noun
Noun || The
The Noun
Noun
me
me || aa Noun
Noun || the
the Noun
Noun
cat
cat || bat
bat || rat
rat
like
like || is
is || see
see || sees
sees
Subject
The
11
Object
Noun
Verb
cat
sees
Noun
rat
.
12
Top-down parsing
Quick review
Syntactic analysis
Lexical analysis
Group letters into words (or group characters into tokens)
Use regular expressions and deterministic FSMs
Grammar transformations
Left-factoring
Left-recursion removal
Substitution
Parsing = structural analysis of program
Group words into sentences, paragraphs, and documents
(or tokens into expressions, commands, and programs)
Top-Down and Bottom-Up
Sentence
Subject
Verb
Object
Noun
The
cat
Noun
sees
rat
13
14
::=
::=
::=
::=
::=
::=
::=
::=
::=
::=
Subject
Subject Verb
Verb Object
Object ..
II || AA Noun
Noun || The
The Noun
Noun
me
me || aa Noun
Noun || the
the Noun
Noun
cat
cat || bat
bat || rat
rat
like
like || is
is || see
see || sees
sees
15
16
public
publicclass
classMicroEnglishParser
MicroEnglishParser{{
private
privatevoid
voidaccept
accept(TerminalSymbol
(TerminalSymbolexpected)
expected){{
ifif(currentTerminal
(currentTerminalmatches
matchesexpected)
expected)
currentTerminal
currentTerminal==next
nextinput
inputterminal
terminal;;
else
else
report
reportaasyntax
syntaxerror
error
}}
private
privateTerminalSymbol
TerminalSymbolcurrentTerminal;
currentTerminal;
//Auxiliary
//Auxiliarymethods
methods will
willgo
gohere
here
...
...
//Parsing
//Parsingmethods
methods will
willgo
gohere
here
...
...
}}
}}
17
...
...
18
Sentence
Sentence ::=
::= Subject
Subject Verb
Verb Object
Object ..
private
privatevoid
voidparseSubject(
parseSubject()){{
ifif(currentTerminal
(currentTerminalmatches
matchesII))
accept(
accept(II););
else
elseifif(currentTerminal
(currentTerminalmatches
matches A
A)){{
accept(
);
accept(AA);
parseNoun(
parseNoun(););
}}
else
elseifif(currentTerminal
(currentTerminalmatches
matchesThe
The)){{
accept(
The););
accept(The
parseNoun(
parseNoun(););
}}
else
else
report
reportaasyntax
syntaxerror
error
}}
private
privatevoid
voidparseSentence(
parseSentence()){{
parseSubject(
parseSubject(););
parseVerb(
parseVerb(););
parseObject(
parseObject(););
accept(.);
accept(.);
}}
19
::=
::= II || AA Noun
Noun || The
The Noun
Noun
20
::=
::= cat
cat || bat
bat || rat
rat
Object
Object
Verb
Verb
private
privatevoid
voidparseNoun(
parseNoun()){{
ifif(currentTerminal
(currentTerminalmatches
matchescat
cat))
accept(
cat););
accept(cat
else
elseifif(currentTerminal
(currentTerminalmatches
matchesbat
bat))
accept(
bat););
accept(bat
else
at))
elseifif(currentTerminal
(currentTerminalmatches
matchesr
rat
accept(
at););
accept(rrat
else
else
report
reportaasyntax
syntaxerror
error
}}
::=
::= me
me || aa Noun
Noun || the
the Noun
Noun
::=
::= like
like || is
is || see
see || sees
sees
private
privatevoid
voidparseObject(
parseObject()){{
}}
??
private
privatevoid
voidparseVerb(
parseVerb()){{
}}
??
21
22
23