Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax analysis and semantic analysis

I am wondering how the syntax analysis and semantic analysis work.

I have finished the lexer and the grammar construction of my interpreter.

Now I am going to implement a recursive descent (top down) parser for this grammar

For example, I have the following grammar:

<declaration>  ::=   <data_type> <identifier> ASSIGN <value>

so i coded it like this (in java):

public void declaration(){
    data_type();
    identifier();
    if(token.equals("ASSIGN")){
        lexer();   //calls next token
        value();
    } else {
        error();
    }
}

Assuming I have three data types: Int, String and Boolean. Since the values for each data types are different, (ex. true or false only in Boolean) how can I determine if it fits the data type correctly? What part of my code would determine that?

I am wondering where would I put the code to:

1.) call the semantic analysis part of my program. 
2.) store my variables into the symbol table.

Do syntax analysis and semantic analysis happen at the same time? or do i need to finish the syntax analysis first, then do the semantic analysis?

I am really confused. Please help.

Thank you.

like image 616
newbie Avatar asked Sep 29 '13 06:09

newbie


People also ask

What is the difference between syntax analysis and semantic analysis?

Syntactic and Semantic Analysis differ in the way text is analyzed. In the case of syntactic analysis, the syntax of a sentence is used to interpret a text. In the case of semantic analysis, the overall context of the text is considered during the analysis.

What is the difference between syntax and semantic?

Put simply, syntax refers to grammar, while semantics refers to meaning. Syntax is the set of rules needed to ensure a sentence is grammatically correct; semantics is how one's lexicon, grammatical structure, tone, and other elements of a sentence coalesce to communicate its meaning.

What is the difference between syntax analyzer & Symantec analyzer?

The main difference between syntax analysis and semantic analysis is that syntax analysis takes the tokens generated by the lexical analysis and generates a parse tree while semantic analysis checks whether the parse tree generated by syntax analysis follows the rules of the language.

What is the difference between lexical analysis and semantic analysis?

From source code, lexical analysis produces tokens, the words in a language, which are then parsed to produce a syntax tree, which checks that tokens conform with the rules of a language. Semantic analysis is then performed on the syntax tree to produce an annotated tree.


1 Answers

You can do syntax analysis (parsing) and semantic analysis (e.g., checking for agreement between <data_type> and <value>) at the same time. For example, when declaration() calls data_type(), the latter could return something (call it DT) that indicates whether the declared type is Int, String or Boolean. Similarly, value() could return something (VT) that indicates the type of the parsed. Then declaration() would simply compare DT and VT, and raise an error if they don't match. (Alternatively, value() could take a parameter indicating the declared type, and it could do the check.)

However, you may find it easier to completely separate the two phases. To do so, you would typically have the parsing phase build a parse tree (or abstract syntax tree). So your top level would call (e.g.) program() to parse a whole program, which would return a tree representing (the syntax of) the program, and you would pass that tree to a semantic_analysis() routine, which would traverse the tree, extracting pertinent information and enforcing semantic constraints.

like image 62
Michael Dyck Avatar answered Sep 28 '22 04:09

Michael Dyck