System Software
System Software
RE ( Regular expression )
NFA ( Non-deterministic Finite Automata )
Parsing
DFA ( Deterministic Finite Automata )
LEX
Today we start
2
Recall: The role of Scanner and Parser
token
Source To semantic
program Scanner Parser analysis
getNextToken
Symbol
table
Programming Language Grammars
• The lexical and syntactic features of a programming language are
specified by its grammar.
alphabets
Words / Strings
Sentences/ Statements
Language
Programming Language Grammars
- Grammar
• A grammar G of a language LG is a quadruple
(Σ, SNT, S, P) where
– Σ is the alphabet
– SNT is the set of NT’s
– S is the distinguished symbol
– P is the set of productions
Programming Language Grammars
- Derivation
• Let production P1 of grammar G be of the
form P1: A ::= α
And let β be such that β = γAθ
β = γαθ
Programming Language Grammars
- Reduction
Programming Language Grammars
- Example
<Sentence> :: = <Noun Phrase> <Verb Phrase>
<Noun Phrase> ::= <Article> <Noun>
<Verb Phrase> ::= <Verb> <Noun Phrase>
<Article> ::= a| an| the
<Noun> ::= boy | apple
<Verb> ::= ate
Programming Language Grammars
<Sentence>
<Noun Phrase> <Verb Phrase>
<Article> <Noun> <Verb Phrase>
<Article> <Noun> <Verb> <Noun Phrase>
the <Noun> <Verb> <Article> <Noun>
the boy <Verb> <Article> <Noun>
the boy ate <Article> <Noun>
the boy ate an <Noun>
the boy ate an apple
Classification of Grammars
• Venn Diagram of Grammar Types:
E E
E + E
0
1 E +
E E E
0 1
1 1
Ambiguity In Grammatic Specification
• Example - I
S → AB | aaB
A → a | Aa
B→b
Check the given grammar is ambiguous or not?
String: aab
Ambiguity In Grammatical Specification
• Example - II
E→E+E
E→E*E
E → id
Check the given grammar is ambiguous or not?
String: id + id * id
Scanning