The Gateway to Computer Science Excellence
+2 votes


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.6k points) | 275 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:


by Veteran (119k points)
edited by

Thanks a lot.. :)

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



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



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. 

where is n+??

moreover , code is in terbo C.

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


bro, what link have u given??

link showing :  page not found



int n+;

syntax error


int n++;

which error??


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.

click the link now, i have activated it..

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

syntax error, that means the error is in compile time


semantic error means error at runtime

ok, thanks :)


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???





@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. 


@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.



@Arjun sir , 

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



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

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

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?

How int 2a; will pass lexical analyzer?
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.


Have u checked the comment section of above link ?

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


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.


@Arjun sir

  int n+  is lexical or syntax error ??? 

plz explain sir?o


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.



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



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.


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,834 questions
57,804 answers
108,254 users