Syntax analyzer will just create parse tree. Semantic Analyzer will check actual meaning of the statement parsed in parse tree. Semantic analysis can compare information in one part of a parse tree to that in another part (e.g., compare reference to variable agrees with its declaration, or that parameters to a function call match the function definition).
Semantic Analysis is used for the following -
- Maintaining the Symbol Table for each block.
- Check Source Program for Semantic Errors.
- Collect Type Information for Code Generation.
- Reporting compile-time errors in the code (except syntactic errors, which are caught by syntactic analysis)
- Generating the object code (e.g., assembler or intermediate code)
Now In the Semantic Analysis Compiler Will Check -
- Data Type of First Operand
- Data Type of Second Operand
- Check Whether + is Binary or Unary.
- Check for Number of Operands Supplied to Operator Depending on Type of Operator (Unary | Binary | Ternary)
Analysis Phase : 2nd Phase of Compiler (Syntax Analysis)
During the first Scanning phase i.e Lexical Analysis Phase of the compiler , symbol table is created by the compiler which contain the list of leximes or tokens.Syntax Analysis :
- It is Second Phase Of Compiler after Lexical Analyzer
- It is also Called as Hierarchical Analysis or Parsing.
- It Groups Tokens of source Program into Grammatical Production
- In Short Syntax Analysis Generates Parse Tree
Parse Tree Generation :
sum = num1 + num2
Now Consider above C Programming statement. In this statement we Syntax Analyzer will create a parse tree from the tokens.
Syntax Analyzer will check only Syntax not the meaning of StatementExplanation : Syntax Analysis
- We know , Addition operator plus (‘+’) operates on two Operands
- Syntax analyzer will just check whether plus operator has two operands or not . It does not checks the type of operands.
- Suppose One of the Operand is String and other is Integer then it does not throw error as it only checks whether there are two operands associated with ‘+’ or not .
- So this Phase is also called Hierarchical Analysis as it generates Parse Tree Representation of the Tokens generated by Lexical Analyzer
No comments:
Post a Comment