2. Syntax Analyzer
• Syntax analysis is a second phase of the compiler design
process that comes after lexical analysis.
• It analyses the syntactical structure of the given input.
• The syntax analyzer also checks whether a given program
fulfills the rules implied by a context-free grammar.
• We have seen that a lexical analyzer can identify tokens
with the help of regular expressions and pattern rules.
3. Syntax Analyzer
• But a lexical analyzer cannot check the syntax of a given sentence due
to the limitations of the regular expressions.
• Regular expressions cannot check balancing tokens, such as
parenthesis.
• Therefore, this phase uses context-free grammar (CFG), which is
recognized by push-down automata.
• CFG, on the other hand, is a superset of Regular Grammar, as
depicted below:
Regular
Grammar
CFG
4. • It implies that every Regular Grammar is also context-free, but there
exists some problems, which are beyond the scope of Regular
Grammar.
• CFG is a helpful tool in describing the syntax of programming
languages.
5. Syntax Analyzer
•A syntax analyzer or parser takes the input from
a lexical analyzer in the form of token streams.
•The parser analyzes the source code (token
stream) against the production rules to detect
any errors in the code. The output of this phase
is a parse tree.
7. Parse Tree
• It is a generalized type of tree which shows pictorially
that how grammar rules are applied on the sequence
of character.
• Parse tree is the output of the syntax analyzer and is
used for designing/defining syntax structure.
• The parse tree describes the syntactic structure of the
input.
8. Syntax Tree
• Compressed form of parse tree which contains operators as internal
node and operands of the operator to be the children of its internal
node.
=
Id +
Id
*
Id
num