Professional Documents
Culture Documents
2
Introduction
In computer programming, a one-pass compiler is a compiler
that passes through the parts of each compilation unit only once,
immediately translating each part into its final machine code.
One pass compiler reads the code only once and then translates it.
A one-pass compiler is fast since all the compiler code is loaded in the
memory at once.
It can process the source text without the overhead of the operating
system having to shut down one process and start another.
3
Introduction
Building one pass compiler involves:
Defining the syntax of a programming language (CFG/BNF)
Generating
Optimize
4
Structure of Compiler
Character Token Intermediate
Syntax-directed
stream Lexical analyzer stream Representation
translator
Develop
parser and code
generator for translator
Syntax definition
(CFG)
5
Syntax Definition
To specify the syntax of a language : CFG and BNF
In CFG, the start symbol is used to derive the string. You can derive the
string by repeatedly replacing a non-terminal by the right hand side of the
production, until all non-terminal have been replaced by terminal symbols.
7
CFG
A CFG recursively defines several sets of strings
One of the non terminals are chosen to denote the language described by the
grammar. This is called the start symbol of the grammar.
Each production describes some of the possible strings that are contained in the set
denoted by a nonterminal.
where N is a nonterminal and X1…Xn are zero or more symbols, each of which is
8
CFG
Some examples:
A→ a
says that the set denoted by the nonterminal A contains the one-
character string a.
A→ aA
9
CFG
10
CFG
Common syntactic categories in programming languages
are:
Expressions:- are used to express calculation of values.
Statements:- express actions that occur in a particular
sequence.
Declarations:- express properties of names used in other parts
of the program.
11
CFG
A CFG Is Characterized By a 4 tuple:
2. A Set of Non-terminals
12
Example CFG
Context-free grammar for simple expressions
List → list-digit
List → digit
Digit → 0|1|2|3|4|5|6|7|8|9
(so we could have written List → list + digit | list - digit | digit )
13
Derivation
A given CFG we can determine the set of all strings(tokens) generated by the
grammar using derivation.
14
Derivation
We begin with the start symbol
2: =>
16
Left-most Derivation
the input is scanned and replaced with the production rule from left to
right. So in left most derivatives we read the input string from left to
right. Example
Production rules:
S=S+S
S=S-S
S = a | b |c
Input : a - b + c
17
Right-most Derivation
The input is scanned and replaced with the production rule from right
to left. So in right most derivatives we read the input string from right
to left.. Example
Production rules:
S=S+S
S=S-S
S = a | b |c
Input : a - b + c
18
Grammars are Used to Derive Strings:
We can derive the string: 9 - 5 + 2 as follows:
→9 - 5 + 2 P4: digit → 2
20
Parse Tree for the Example
Grammar
21
Ambiguity
A grammar is said to be ambiguous if there exists more than one left
most derivation or more than one right most derivation or more than
one parse tree for a given input string.
with production P =
22
Ambiguity
Two derivations (Parse Trees) for the same token string.
23
Associativity of Operators
➢ An operator is left-associative if the expression abc must be evaluated
from left to right, i.e., as (ab)c .
24
Associativity of Operators
• Left Associative Grammar • Right Associative Grammar
list → | list – digit right → letter = right | letter
digit →0|1|…|9 letter → a|b|…|z
25
Precedence of Operator
➢ A possible way of resolving the ambiguity is to use
precedence rules during syntax analysis to select among the
possible syntax trees.
26
Precedence of Operator
expr term
number number
2 + 3 * 5 27