Parser Lec1
Parser Lec1
Contents
• Syntax Analysis
• Introduction
• The Role of the Parser
• Representative Grammars
• Context-Free Grammars
• Formal Definition of a CFG
• Notational Conventions
• Derivations
2
Syntax Analysis
• Grammars offer significant benefits for both language
designers and compiler writers.
4
Role of the Parser..
• There are 03 general types of parsers for grammars:
universal, top-down, and bottom-up.
• Universal parsing methods such as the Cocke-Younger-Kasami
algorithm and Earley's algorithm can parse any grammar. These
general methods are, however, too inefficient to use in
production compilers.
• Top-down methods build parse trees from the top (root) to the
bottom (leaves), while bottom-up methods start from the leaves
and work their way up to the root.
5
Representative Grammars
• Expressions with + and *
E→E+T|T
T→T*F|F
F → ( E ) | id
E → E + E | E * E | ( E ) | id
• This grammar does not enforce precedence and it does not
specify left vs right associativity.
For example, id + id + id and id * id + id each have two parse
trees.
7
CFG
• Grammars used to systematically describe the syntax of
programming language constructs like expressions and
statements.
• Other productions then define precisely what an expr is and what else
a stmt can be.
• Terminals:
• The basic components found by the lexer.
• They are sometimes called token names, i.e., the first
component of the token as produced by the lexer.
• Non-terminals:
• Syntactic variables that denote sets of strings.
• The sets of strings denoted by non-terminals help define the
language generated by the grammar
9
CFG Definition..
• Start Symbol:
• A non-terminal that forms the root of the parse tree.
• Conventionally, the productions for the start symbol are listed
first.
• Productions:
• The productions of a grammar specify the manner in which the
terminals and non-terminals can be combined to form strings.
11
CFG Definition..
• Ex Grammar
• Terminals: id + - * / ( )
• Non-Terminals: expression, term, factor
• Start Symbol: expression
12
Notational Conventions
• Notational conventions for grammars:
13
Notational Conventions..
14
Notational Conventions…
• Uppercase letters late in the alphabet, such as X, Y, Z, represent
grammar symbols, that is, either non-terminals or terminals.
16
Derivations
• Assume we have a production A → α.
• We would then say that A derives α and write A ⇒ α
17
Derivations
• Formal definition of zero or more definitions:
1. α ⇒* α, for any string α.
2. If α ⇒* β and β ⇒ γ, then α ⇒* γ.
19
Thank You