0% found this document useful (0 votes)
28 views

Lec 03 Syntax Analysis

Uploaded by

zainfayyaz498
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lec 03 Syntax Analysis

Uploaded by

zainfayyaz498
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Syntax Analysis in Compiler Construction

Syntax analysis, also known as parsing, is the second phase of the


compilation process following lexical analysis. It focuses on checking the
syntactic structure of the source code to ensure that it conforms to the
grammar rules of the programming language. The output of this phase is a
parse tree or syntax tree that represents the hierarchical structure of the
source cod
Role of Syntax Analysis:
 Input: The sequence of tokens generated by the lexical analyzer.
 Output: A parse tree or syntax tree that reflects the syntactic structure of the source
code.
 Objective: To check whether the token sequence adheres to the syntactic rules
defined by the language grammar.
Syntax Analysis in Compiler Construction
 For any input string which is given in the form of stream of token for
the parser, if the derivation tree exist, then the input string is
syntactically or grammatically correct.
 If the parser cannot generate the derivation tree from the I/P string,
then there must be some grammatically mistake in the string.
Types of Parsers
 Top-Down Parsers:
 Start from the root and attempt to derive the input string using the
grammar rules.
 Recursive Descent Parsing:
A kind of top-down parser that uses a set of recursive procedures to
process the input.
 Simple to implement but struggles with left-recursive grammars.
 Predictive Parsing:
 A more efficient type of top-down parsing that uses lookahead tokens to
predict the correct production rule.
 Typically implemented using LL(1) parsers, where "1" indicates one
token of lookahead.
Types of Parsers
 Bottom-Up Parsers:
 Start from the input symbols and attempt to construct the parse tree by reducing
substrings to non-terminals.
 Shift-Reduce Parsing:
 Uses a stack to manage symbols and a set of rules to decide whether to shift (read next
input) or reduce (replace symbols with a non-terminal).
 Can handle a broader range of grammars than top-down parsers.
 LR Parsing:
 A powerful and efficient bottom-up parsing technique, where "LR" stands for "Left-to-
right scanning of input" and "Rightmost derivation in reverse".
 Variants include SLR (Simple LR), LALR (Look-Ahead LR), and CLR (Canonical LR).
Handling Ambiguities
 A grammar is ambiguous if there is more than one parse tree for a
given string. Ambiguities can lead to multiple interpretations of the
source code, which is undesirable.
 Example of Ambiguity: For the grammar:
 E → E + E | E * E | id
 The expression id + id * id can be parsed in two ways:
 (id + id) * id
 id + (id * id)
Solutions to Ambiguity:
 Refactor the Grammar: Modify the grammar to eliminate
ambiguity.
 Use Precedence and Associativity Rules: Define precedence and
associativity to guide the parser in choosing the correct parse tree.
 Parser Directives: Some parser generators allow directives to handle
specific ambiguities.
Error Handling in Syntax Analysis
 A crucial aspect of syntax analysis is handling syntax errors effectively.
The parser should detect errors gracefully and provide meaningful
messages to aid in debugging.
 Error Recovery Strategies:
 Panic Mode: Skip input symbols until a synchronizing token is found, then
resume parsing.
 Phrase-Level Recovery: Replace or insert symbols in the input to continue
parsing.
 Error Productions: Augment the grammar with productions that can handle
common errors.
 Global Correction: Attempt to find the minimum number of changes required
to correct the input.
Constructing a Syntax Analyzer
To build a syntax analyzer, one needs to:
 Define the Grammar: Specify the CFG for the language.
 Choose a Parsing Strategy: Decide between top-down and
bottom-up parsing based on grammar characteristics.
 Implement or Generate the Parser: Use tools like YACC/Bison
(for LR parsers) or ANTLR (for LL parsers).
 Incorporate Error Handling: Implement robust error recovery
mechanisms.
Conclusion
Syntax analysis is a foundational step in compiler construction,
transforming a linear sequence of tokens into a hierarchical structure
that captures the syntactic relationships of the source code. By
understanding the types of parsers, handling ambiguities, and
implementing effective error handling, one can create a robust syntax
analyzer capable of parsing complex programming languages.
Precedence of Operators in Compiler
Construction
Operator Precedence:
Precedence defines the order in which different operators in an
expression are evaluated. Operators with higher precedence are
evaluated before operators with lower precedence. For instance, in the
expression 3 + 4 * 5, the multiplication (*) has higher precedence than
the addition (+), so 4 * 5 is evaluated first, resulting in 3 + 20 = 23.
Key Points:
 Precedence Levels: Operators are grouped into levels of precedence.
Each level is evaluated before the levels below it.
 Usage in Parsing: Precedence helps in resolving the ambiguity in
expressions without the need for parentheses.
 Precedence Table: Most programming languages define a
precedence table that specifies the precedence level of each operator.
Table of Precedence level
Operators Description Precedence

`*` `/` `%` Multiplication, etc. High

`+` `-` Addition, Subtraction Medium

`<` `>` `<=` Relational Operators Low

`==` `!=` Equality Operators Lower

`&&` Logical AND Even Lower

`||` Logical OR Lowest


Associativity of Operators in Compiler
Construction
Associativity defines the order in which operators of the same
precedence level are evaluated. There are two primary types:
 Left-Associative (Left-to-Right): Operators are evaluated from
left to right.
 Most arithmetic operators (like +, -, *, /) are left-associative.
 For example, 10 / 2 * 5 is evaluated as (10 / 2)* 5 = 25.
 Right-Associative (Right-to-Left): Operators are evaluated from
right to left.
Most arithmetic operators (like +, -, *, /) are left-associative.
For example, 10 / 2 * 5 is evaluated as 10 / (2 * 5) = 1
Precedence and Associativity Table
Category Operators Associativity
Parenthesis/ Brackets () [] -> , ++ -- Left to Right
Unary ! ~ ++ -- + * & (type) Sizeof Right to Left
Multiplicative */% Left to Right
Additive +- Left to Right
Bitwise Shift << >> Left to Right
Relational < <= > >= Left to Right
Equality ==!= Left to Right
Bitwise AND & Left to Right
Bitwise XOR ^ Left to Right
Precedence and Associativity Table
Category Operators Associativity
Bitwise OR | Left to Right
Logical AND && Left to Right
Logical OR || Left to Right
Conditional ? : +- Right to Left
Assignment = += -= *= /= %= &= ^= |= Right to Left
<<= >>=
Comma , Left to Right
Conclusion
In summary, understanding and correctly implementing the rules of
precedence and associativity is critical in designing a parser that can
accurately interpret and evaluate expressions. These rules help resolve
ambiguities and ensure that the parsed structure aligns with the expected
mathematical or logical interpretation of expressions in a given
programming language
Any Question?

You might also like