Lecture 03
Lecture 03
Compiler Construction
CS-4207
Lecture – 03
Disclaimer: The Contents of this reader are borrowed from the book(s) mentioned
in the reference section.
Introduction
As we discussed in the previous lecture that structurally compiler has two part: Analysis
that breaks up a source program into constituent pieces and produces an internal
representation for it, called intermediate code, and synthesis, that translates the
intermediate code into the target program. On the basis of this division the Analysis part
is referred as front end of compiler while the synthesis part is referred as back end of
compiler.
Analysis is organized around the “syntax" and “semantics” of the language to be
compiled. The syntax of the language describes the correct form in which its programs
should be written, while, the semantics of a programming language describes what its
programs signify, i.e., what each program does when it is executed. The Syntax is
specified using Context Free Grammar, whereas, the semantics are difficult to express
and we need to informal descriptions and suggestive examples. A grammar oriented
compiling technique is known as Syntax-Directed translation.
2|Page
Dr. Atif Ishaq Khan – Assistant Professor GC University, Lahore
3|Page
Dr. Atif Ishaq Khan – Assistant Professor GC University, Lahore
Example of Derivation
Let’s derive the string 9 − 5 + 2 as follows [2]
Where the root node is start symbol, leaf nodes are terminals, while the interior nodes
are non-terminals.
Example of Derivation
Consider the example 9 – 5 + 2
Here we have another definition of the language generated by the grammar is that consists
of set of strings that can be generated by some parse tree. The process of finding a parse
tree fro given string of terminals is called parsing of string.
What is ambiguity in grammar?
A grammar can have more than one parse tree generating a given string of terminals. If
there is possibility of more than one derivations for a string then the grammar is said to
be ambiguous. Since a string with more than one parse tree usually has more than one
meaning, we need to design unambiguous grammars for compiling applications, or to use
4|Page
Dr. Atif Ishaq Khan – Assistant Professor GC University, Lahore
ambiguous grammars with additional rules to resolve the ambiguities. The following
grammar is ambiguous
→ +
→ –
→ 0|1|2|3|4|5|6|7|8|9
Now we can generate more than one parse tree for the same string 9 – 5 + 2
Associativity of Operators
Associativity is the left-to-right or right-to-left order for grouping operands to
operators that have the same precedence. An operator's precedence is meaningful only
if other operators with higher or lower precedence are present. Expressions with higher-
precedence operators are evaluated first [3].
Operators are either left-associative or right-associative. If in any expression the operators
of the same precedence are used then there is no issue of associativity.
For example, 9 + 5 + 2 is equivalent to (9 + 5) + 2 and 9 – 5 – 2 is equivalent to
(9 – 5) – 2. The four arithmetic operators, addition, subtraction, multiplication, and
division are left associative. An operand with + sign on its both sides belongs to the
operator to its left.
A grammar [2] such as
→ + |
→ 0 | 1 | 2 | 3 |4 | 5 | 6 | 7 | 8 | 9
Is called left recursive and captures left associativity.
It will parse string 1 + 2 + 3 + 4 as
{{1 + 2} +3 } + 4
In contrast the grammar [2]
→ ∶= | ≔
→ |… … . . |
→ 0 | 1 | 2 | 3 |4 | 5 | 6 | 7 | 8 | 9
5|Page
Dr. Atif Ishaq Khan – Assistant Professor GC University, Lahore
Solution
We need to define our grammar in such way that the issue of precedence and precedence
must not create any ambiguity.
For example we can take two different variables and to define the two
difference precedence and an extra variable for generating the basic units (digits
and parenthesized expressions) in expressions.
This grammar resolves the issue of associativity as well the precedence and we can nest
the expression using parenthesis.
6|Page
Dr. Atif Ishaq Khan – Assistant Professor GC University, Lahore
References
1. Alferd. V. Aho, Monica, S Lam, Ravi Sethi, and Jeffry D. Ullman, “Compilers,
Principles, Techniques and Tools”, Chapter-2, Second Edition, Pearson,
2. Honor Compilers, NYU, Lecture 1, Fall 2009, A. Pnueli
3. https://www.ibm.com/docs/en/xl-c-and-cpp-aix/11.1.0?topic=operators-operator-
precedence-associativity
7|Page