retagged by
4,336 views
3 votes
3 votes
1. Declaring an array with a floating point subscript :

               e.g    int a[2.8] ;

In my opinion ,it should be a syntax error :

How will the parse tree be generated for this ,because in between the square brackets we can have an identifier with some constant literal so in the grammar we can have S->int a[id] where id can be a constant literal , so why can't this be a syntax error , why it has to be semantic only ?

 

2. Simple declaration of a variable :

e.g int a ;

Now it is said that for simple declarative sentences ,parse tree is not generated , but then if I would have written int 1 ; then in this case ,if parse tree is not generated then how will the compiler know whether the token is a constant or an identifier ?

3. If there is an undefined symbol in the code , then why it can't be detected during parse tree and how is it detected during semantic phase only ?

4. If the name of the identifier is too long , then is this any type of error ?If Yes , then in which phase is this detected ?
retagged by

1 Answer

Best answer
3 votes
3 votes
Syntax Analysis: Checks out if the code follows the given CFG or not, i.e., it just checks the given code for typing errors based on the programming language's rules. For eg., if we miss a terminator/semicolon at the end of a C program's statement then it will be a syntax error because C requires its each statement/line to end with a terminator symbol. And for this purpose, a parse tree is generated on the basis of the given CFG.

Semantic analysis: Checks the meaning & usefulness of the program, i.e., type checking. It is better understood by examples:

1. x=a+b*c;

Now "x" can't be a constant or an array.

2. int a[3]={1,2,3};

int b[3]=a[3];

This is invalid because an array can not be directly assigned to some other array. This is semantically wrong but its syntax is alright.

While semantic analysis certainly can not check all possible meanings of all codes so it has its own limitations. But in reality, all these phases have vague lines to differentiate them. Compilers are implemented just as frontend(analysis phase) and backend(synthesis phase). That's why all operations of lexical, syntax  & semantic analysis are merged and it is hard to say what happened in which phase unless we know how the compiler itself was implemented at the first phase. One could try checking out output after each phase(there are specific terminal commands for them) but then this could vary when checked on a different machine.

NOW, coming to your questions:

1. Parse tree will be generated for it but it won't be semantically verified because it is meaningless. It is like those examples I gave above in semantic analysis. Thus, this error is only found out in semantic analysis phase.

2. I am hearing this for the first time that a parse tree is not generated for simple declarative sentences. I think int a; will also generate a parse tree. Please provide a link of where you read it, I could be wrong.

3. If an undefined symbol is used without creating a syntax error then it is found out in semantic analysis because syntax analysis just checks the rules of a programming language but not its meaning. But it is vague(maybe wrong) to say so because syntax and semantic phase, as said previously, are implemented together.

4. It is detected in lexical analysis phase. Errors detected by lexical analysis phase:

- Numeric literals too long. Eg. int a=12345678; (exceeding integer range)

- Long identifiers

- Badly formed numeric literals. Eg. int a=$123; ($ not allowed)

- Input characters that are not in the same language. Eg., On a english fixed (only) compiler, one entered/copied a Hindi letter.
selected by

Related questions

2 votes
2 votes
3 answers
1
pC asked Nov 19, 2015
2,663 views
0 votes
0 votes
0 answers
2
smsubham asked Dec 3, 2018
445 views
What are the Errors Detected in the Semantic Phase explain with Examples?Some I know is type checking (incorrect variable type), non existent variables. array subscripts ...
1 votes
1 votes
1 answer
4
rahul sharma 5 asked Apr 28, 2018
1,442 views
int a = b+*/c;Which phase will give the error?Lexical or syntax?