Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantic analysis in compilers [closed]

How is the semantic analysis done by a compiler (generally)?

I had to answer to this question during my last exam, it wasn't enough for the professor.

I included BNF (with an example) and syntactic cards in my answer, to which he asked me: "What happens when the compiler finds a statement like int i;?"

like image 345
Overflowh Avatar asked Jan 03 '12 14:01

Overflowh


People also ask

What does semantic analysis do in a compiler?

Semantic Analysis is the third phase of Compiler. Semantic Analysis makes sure that declarations and statements of program are semantically correct. It is a collection of procedures which is called by parser as and when required by grammar.

What are the three types of semantic analysis?

Semantic analysis is examined at three basic levels: Semantic features of words in a text, Semantic roles of words in a text and Lexical relationship between words in a text.

What are semantic errors in compiler?

A semantic error is text which is grammatically correct but doesn't make any sense. An example in the context of the C# language will be “int x = 12.3;” - 12.3 is not an integer literal and there is no implicit conversion from 12.3 to int, so this statement does not make sense. But it is grammatically correct.

Can compiler detect semantic errors?

All syntax errors and some of the semantic errors (the static semantic errors) are detected by the compiler, which generates a message indicating the type of error and the position in the Java source file where the error occurred (notice that the actual error could have occurred before the position signaled by the ...


1 Answers

Time to read Aho&Ullman/Dragon book carefully.

Semantic analysis is the activity of a compiler to determine what the types of various values are, how those types interact in expressions, and whether those interactions are semantically reasonable. For instance, you can't reasonably multiply a string by class name, although no editor will stop you from writing

 "abc" * MyClass

To do this, the compiler must first identify declarations and scopes, and typically records the result of this step in a set of symbol tables. This tells it what specific identifiers means in specific contexts. It must also determine the types of various literal constants; "abc" is a different type than 12.2e-5.

Then it must visit all locations where identifiers and literals are used, and verify that the use of the identifier/literal, and the results computed, are compatible with the language definition (as in the above example).

As to how this is done: typically the source code is parsed, some representation of the program is constructed (syntax trees are very popular), and that representation is walked ("visited") element by element to collect/validate the semantic information. The symbol table is usually just a set of hash tables associated with the syntax tree representing a scope, hashing from identifiers to structures containing type declarations.

like image 186
Ira Baxter Avatar answered Nov 15 '22 08:11

Ira Baxter