The Gateway to Computer Science Excellence
+2 votes
207 views

 

The above diagram is Transition Diagrams for identifiers. As we can see that the identifier is said to be accepted if it starts with a letter and ends with a valid delimiter, which includes blank symbol, arithmetic, logical operator, left parenthesis, right parenthesis, +, :, ; etc.

Now say i declare an identifier int 4n; I understand that this (4n) is not a valid identifier as it starts with a number..

Now say i declare another identifier int n+ ; Does lexical analyzer accept it? I know that it will give compilation error but that is not my interest, i solely want to know will this be accepted by the lexical analyzer or not?

P.S→ I think it will because identifier ends with a delimiter and + is a valid delimiter and the error in declaration will not be detected at this stage...

in Compiler Design by Active (3.5k points) | 207 views

1 Answer

0 votes
int 4n//lexical error

Here $4n$ is valid two token (4 and n), but lexical analyzer cannot recognize it as valid pattern.So, it will give lexical error.


int n+;//syntax error.

Because , 'n+' not matching with any C program syntax. So, it will give syntax error.


Lexical error- Pattern mismatch.

Syntax error- Symbolic representation

Semantic error-Meaning 

Detail here:https://stackoverflow.com/questions/34834926/difference-between-lexical-and-syntax-error

https://softwareengineering.stackexchange.com/questions/113800/whats-the-difference-between-syntax-and-semantics

 

by Veteran (117k points)
edited by
0
0

Thanks a lot.. :)

So int n+ is not considered as either lexical error and syntax error right?

0

@srestha

And are these identifier rules also applies for GCC compiler as well?

 

+1

int n+ is not considered as either lexical error and syntax error right?

yes :)

 

And are these identifier rules also applies for GCC compiler as well?

think so. 

0
where is n+??

moreover , code is in terbo C.

donot use terbo-C compiler. It has lots of error.
0

@Hirak

bro, what link have u given??

link showing :  page not found

0

if 

int n+;

syntax error

then 

int n++;

which error??

0

int n+;

has 4 token. int ,  n  ,  ,  ;

So, no lexical error.

Now, we have to check syntax error.

If some expression  missing in it?? If so then syntax error.

https://wci.llnl.gov/codes/basis/manual/node53.html

+1
click the link now, i have activated it..
+1

as i have given n+ together so i think that should be a syntax error..

0
syntax error, that means the error is in compile time

and

semantic error means error at runtime

right??
0
ok, thanks :)
0

@srestha

can you tell me 

  1. int 4n;  //lexical error
  2. int n+; //syntax error.

there is error in this statment okay but instead of that if question is how many tokens in this two statments.

we can find token or not???

 

 

 

+1
yes..
+2

@srestha "

int n+ is not considered as either lexical error and syntax error right?

You are saying both "yes" and "no" to this.

Both syntax and semantic errors are detected at compile time. Please refer standard text books when in doubt. 

0

@srestha mam, 

int 4n; will give lexical error because 4n cannot be matched with any valid pattern. 

But int 4; will give syntax error during parsing. Lexical analyzer will generate valid tokens.

int n+; will give lexical error as lexical analyzer will not be able to group n and + into lexeme and generate  token identifier.  On seeing first n lexical analyzer will check with the pattern of identifier, but on seeing + it will give lexical error.

 

0

@Arjun sir , 

sir please check my above comment, whether it is right or wrong

0

@SuvasishDutta

Is '+' not a valid token? Why  int n+; will give lexical error?

+3
Yes. int n+; should pass lexical analyser as when + is encountered DFA for operator will start and that also passes when ; comes.
+1

Even I read 

int 2a; also  valid token, where 2,a considered as two tokens.

But will it not violating 'valid identifier' rules? Then where do we check valid identifier?

0
How int 2a; will pass lexical analyzer?
0
int 2a can never pass lexical analysis phase, the automaton provided in the question will immediately detect an error.

But int n+ will definitely pass.
0

@Hirak

Have u checked the comment section of above link ?

https://stackoverflow.com/questions/34834926/difference-between-lexical-and-syntax-error

though nothing found in dragon book. In that book no clear idea which would give compiler error-syntax sematic etc :(

+1

In the comment section it is mentioned that --> 2ab is not a valid C token. (Note that 2ab is a valid C preprocessing token that can be used in token pasting macros)

I do not understand the difference between invalaid token and valid preprocessing token, but one thing for sure int 2ab will give lexical error, the automata itself says the same.

0

@Arjun sir

  int n+  is lexical or syntax error ??? 

plz explain sir?o

+1

int n+; will pass the lexical analyzer. 

Tokens generated are:

1. Keyword- int

2. Identifier- n

3. Operator- +

4. Delimiter/special symbol-  ;

Concept behind tokenization:

When the lexical analyzer read the source-code, it scans the code letter by letter; and when it encounters a whitespace, operator symbol, or special symbols, it decides that a word is completed.

On receiving the above tokens, syntax analyzer will give error during parsing as the statement cannot be derived from the context free grammar of delaration.

0

@SuvasishDutta

n+ as whole will be an identifier for this question.(for the given finite automata)

 

0
No.

It is applicable to any question. Here also n is indentifier and + will be operator. So two tokens will be generated for n+.

I have mentioned the concept used in my previous post.
0

@srestha

So, what's the final answer.

I read all the comments but still not a clear picture.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,645 questions
56,597 answers
195,837 comments
102,137 users