search
Log In
9 votes
1.5k views
When do we count ++(post increment or pre increment) as 1 or 2 token ?
in Compiler Design
edited by
1.5k views
0
All operators are tokens, hence ++ is taken as a single token.

Correct if wrong.
6

This will be consider as single token, because compiler apply greedy approach for performing tokenization, it will try to include more character in token if possible.

https://gcc.gnu.org/onlinedocs/cpp/Tokenization.html

0

@ Shubhanshu Thanks it helps :) /\

3 Answers

8 votes
 
Best answer
Preprocessor follows greedy approach to tokenize,  This can be done in TWO steps

1 => Separated by space (A+ ++B) => Have 4 tokens(Space is not considered as token), here tokens are A, +, ++, B(In order of expression). If any operator doesn't have any space ,

then ,

2 => Greedy approach (A+++B) for longest possible valid operator in the Grammer => Like we have '++' > '+' so here the tokens are A,++, +, B(In order of expression).

selected by
0

@Harish Kumar 2 I think there is nothing like priority in tokenization.

While performing tokenization, Compiler use greedy approach and tries to include more token if possible,

ref https://gcc.gnu.org/onlinedocs/cpp/Tokenization.html

0
Ok, so after Lexical analysis then it checks then the compiler checks which operator to be formed?

Like for "A+++B"(if used some where) then, how many tokens? and how will it use this. Or space is used to seperate these now. Kindly clarify.
0

Then tokens are :-

A

++

+

B

Even you can run on ide

int main()
{
    int a,b;
    a = 10;
    b = 10;
    printf("Hello World %d \nval of a = %d\nval of b = %d", (a+++b), a, b);
    return 0;
}

Output

Hello World 20

val of a = 11

val of b = 10

0
So then priority plays role in getting tokens, right? Because if not, then by greedy approach it should have considered it as '+' only, Shouldn't it???
0
For tokenization, greedy is used and while solving the expression,

like a+++b

priority of operators are used because unary operator have higher precedence than binary operator.
0
Ok thanks Shubhansu, now I have got it.

And i have updated the answer, can you please look into that now and provide feedback.
0

@Harish Kumar 2 now it is correct.

0
Can anyone explain no. of tokens in case of unary operator such as the below line

int a = -3;

Is -3 is taken as single token or two tokens?
0
-3 is 2 token
0
how many tokens in:

 a+=b;
5 votes
Always when we count the token complier always see next symbol

Eg-

++= 1token

a+++++b =5tokens

&&= 1token

edited by
0 votes
I think 2 tokens because count is an identifier and ++ is an operator.

Related questions

6 votes
1 answer
1
458 views
Is this line a/*b successfully generating the token's or it give lexical error ? if it is given the lexical error then why so ? I think it will give lexical error because we don't find */ here
asked Jan 12, 2018 in Compiler Design junaid ahmad 458 views
1 vote
1 answer
2
1.3k views
Find no of tokens in below program. #include<stdio.h> main() { int I; int *pi = &I; //parent pointer scanf("%d",pi); printf("%d\n", I+5); }
asked Sep 21, 2018 in Compiler Design sagar27 1.3k views
2 votes
1 answer
3
643 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, +, ... ends with a delimiter and + is a valid delimiter and the error in declaration will not be detected at this stage...
asked Jun 9, 2019 in Compiler Design Hirak 643 views
...