LATEST UPDATE (ALREADY GRADED A+)
Syntax Analyzer / Parser
Groups sequences of tokens from
the lexical analysis phase into
phrases, each with an associated
phrase type (i.e., a logical unit with respect to the rules of the source language)
Operations performed in the syntax analyzer phase:
-Obtain tokens from lexical analyzer
-Check whether the expression is syntactically correct
-Report syntax errors, if any
-Determine the statement class
•An assignment statement, a condition statement, etc.
-Group tokens into statements
-Construct hierarchical structures called parse trees
•Parse trees represent the syntactic structure of the program
Semantic Analyzer
Takes its input from the syntax analysis phase in the form of a parse tree and a symbol table
Its purpose is to determine if the input has a well-defined meaning
In practice, semantic analyzers are mainly concerned with type checking and type coercion based on
type rules
Functions performed by the semantic analyzer
Check phrases for semantic errors
Keep track of types of identifiers and expressions to verify the consistent usage
Maintains symbol table
Lexical Analyzer / Scanner
Scans the source code from left-to-right,
character-by-character, and groups these characters into lexemes (sequence of characters that match a
pattern) and outputs a sequence of tokens to the syntax analyzer.
Main functions of Lexical Analyzer Phase:
, Identify the lexical units in a source statement
Classify units into different lexical classes (e.g., constants, reserved words, etc.) and enter them in
different tables
Build a descriptor (called a token) for each lexical unit
Ignore comments in the source program
Detect tokens which are not a part of the language
Regular Expressions: (0|1)*
All strings of 1s and 0s
Regular Expressions: 1(0|1)*
Regular Expressions: ( 0 | 1 )1 1( 0 | 1 )
All strings of 0s and 1s containing at least two consecutive 1s
Regular Expressions: ( ε | 1 ) ( 0 1 )* ( ε | 0 )
All strings of alternating 0s and 1s
Symbol Table
Built and maintained by the semantic analyzer
Maps each identifier to information known about it:
-Identifiers type (e.g., int, char, float, etc.)
-Internal structure (if any)
-Scope (the portion of the program in which it is valid)
Using the symbol table, the semantic analyzer enforces a large variety of rules
Purpose of symbol table is to provide quick and uniform access to identifier attributes throughout
compilation process
Optimizing Transformations
Constant folding
return 3 + 5 -> return 8
Dead code elimination
unused variables
Common Sub-Expression elimination
a = b c........x = b c + 5