Redirected
2,464 views
7 votes
7 votes

5 Answers

6 votes
6 votes
The program number 1 has an 'unknown' next to '@' . Clearly an error.

 

~/Downloads$ clang -Xclang -dump-tokens 1.c
int 'int' [StartOfLine]Loc=<b.c:1:1>
identifier 'main' [LeadingSpace]Loc=<b.c:1:5>
l_paren '('Loc=<b.c:1:9>
r_paren ')'Loc=<b.c:1:10>
l_brace '{'Loc=<b.c:1:11>
int 'int' [StartOfLine]Loc=<b.c:2:1>
identifier 'a' [LeadingSpace]Loc=<b.c:2:5>
equal '='Loc=<b.c:2:6>
numeric_constant '3'Loc=<b.c:2:7>
semi ';'Loc=<b.c:2:8>
unknown '@' [StartOfLine]Loc=<b.c:3:1>
identifier '_'Loc=<b.c:3:2>
return 'return' [StartOfLine]Loc=<b.c:4:1>
numeric_constant '0' [LeadingSpace]Loc=<b.c:4:8>
semi ';'Loc=<b.c:4:9>
r_brace '}' [StartOfLine]Loc=<b.c:5:1>
eof ''Loc=<b.c:5:2>

 

The program number 2 has no issues in lexer. All lexems were identified by the lexer.

~/Downloads$ clang -Xclang -dump-tokens b.c
int 'int' [StartOfLine]Loc=
identifier 'main' [LeadingSpace]Loc=
l_paren '('Loc=
r_paren ')'Loc=
l_brace '{'Loc=
int 'int' [StartOfLine]Loc=
identifier 'a' [LeadingSpace]Loc=
equal '='Loc=
numeric_constant '3'Loc=
semi ';'Loc=
if 'if' [StartOfLine]Loc=
l_paren '('Loc=
identifier 'a'Loc=
less '<' [LeadingSpace]Loc=
numeric_constant '66' [LeadingSpace]Loc=
identifier 'a' [LeadingSpace]Loc=
equal '='Loc=
numeric_constant '9'Loc=
semi ';'Loc=
r_brace '}' [StartOfLine]Loc=
eof ''Loc=

 

 

The number 3 was cleary a error, lexer kept waiting for end of comment and found eof.

~/Downloads$ clang -Xclang -dump-tokens 3.c
int 'int' [StartOfLine]Loc=<b.c:1:1>
identifier 'main' [LeadingSpace]Loc=<b.c:1:5>
l_paren '('Loc=<b.c:1:9>
r_paren ')'Loc=<b.c:1:10>
l_brace '{'Loc=<b.c:1:11>
int 'int' [StartOfLine]Loc=<b.c:2:1>
identifier 'a' [LeadingSpace]Loc=<b.c:2:5>
equal '='Loc=<b.c:2:6>
numeric_constant '3'Loc=<b.c:2:7>
semi ';'Loc=<b.c:2:8>
b.c:3:1: error: unterminated /* comment
/* hello
^
eof ''Loc=<b.c:6:2>
1 error generated.

The number 4 is totally fine with lexer. you can see all the token identified by lexer. 09 is a numeric_constant.

~/Downloads$ clang -Xclang -dump-tokens 4.c
int 'int' [StartOfLine]Loc=<a.c:1:1>
identifier 'main' [LeadingSpace]Loc=<a.c:1:5>
l_paren '('Loc=<a.c:1:9>
r_paren ')'Loc=<a.c:1:10>
l_brace '{' [LeadingSpace]Loc=<a.c:1:12>
int 'int' [StartOfLine] [LeadingSpace]Loc=<a.c:2:2>
identifier 'a' [LeadingSpace]Loc=<a.c:2:6>
equal '=' [LeadingSpace]Loc=<a.c:2:8>
numeric_constant '09' [LeadingSpace]Loc=<a.c:2:10>
semi ';'Loc=<a.c:2:12>
return 'return' [StartOfLine] [LeadingSpace]Loc=<a.c:3:2>
numeric_constant '0' [LeadingSpace]Loc=<a.c:3:9>
semi ';'Loc=<a.c:3:10>
r_brace '}' [StartOfLine]Loc=<a.c:4:1>
eof ''Loc=<a.c:4:2>

 

2 votes
2 votes

 

 

1 and 3 produces error during Lexical Analysis. Rest 2 and 4 are fine.

Details below with a real lexical analyser. No need to guess when you have lexical analysers at your finger tips.

 

 

The program number 1 has an 'unknown' next to '@' . Clearly an error.

 

~/Downloads$ clang -Xclang -dump-tokens 1.c
int 'int' [StartOfLine]Loc=<b.c:1:1>
identifier 'main' [LeadingSpace]Loc=<b.c:1:5>
l_paren '('Loc=<b.c:1:9>
r_paren ')'Loc=<b.c:1:10>
l_brace '{'Loc=<b.c:1:11>
int 'int' [StartOfLine]Loc=<b.c:2:1>
identifier 'a' [LeadingSpace]Loc=<b.c:2:5>
equal '='Loc=<b.c:2:6>
numeric_constant '3'Loc=<b.c:2:7>
semi ';'Loc=<b.c:2:8>
unknown '@' [StartOfLine]Loc=<b.c:3:1>
identifier '_'Loc=<b.c:3:2>
return 'return' [StartOfLine]Loc=<b.c:4:1>
numeric_constant '0' [LeadingSpace]Loc=<b.c:4:8>
semi ';'Loc=<b.c:4:9>
r_brace '}' [StartOfLine]Loc=<b.c:5:1>
eof ''Loc=<b.c:5:2>
 

The program number 2 has no issues in lexer. All lexems were identified by the lexer.

~/Downloads$ clang -Xclang -dump-tokens b.c
int 'int' [StartOfLine]Loc=
identifier 'main' [LeadingSpace]Loc=
l_paren '('Loc=
r_paren ')'Loc=
l_brace '{'Loc=
int 'int' [StartOfLine]Loc=
identifier 'a' [LeadingSpace]Loc=
equal '='Loc=
numeric_constant '3'Loc=
semi ';'Loc=
if 'if' [StartOfLine]Loc=
l_paren '('Loc=
identifier 'a'Loc=
less '<' [LeadingSpace]Loc=
numeric_constant '66' [LeadingSpace]Loc=
identifier 'a' [LeadingSpace]Loc=
equal '='Loc=
numeric_constant '9'Loc=
semi ';'Loc=
r_brace '}' [StartOfLine]Loc=
eof ''Loc=

 

 

The number 3 was cleary a error, lexer kept waiting for end of comment and found eof.

~/Downloads$ clang -Xclang -dump-tokens 3.c
int 'int' [StartOfLine]Loc=<b.c:1:1>
identifier 'main' [LeadingSpace]Loc=<b.c:1:5>
l_paren '('Loc=<b.c:1:9>
r_paren ')'Loc=<b.c:1:10>
l_brace '{'Loc=<b.c:1:11>
int 'int' [StartOfLine]Loc=<b.c:2:1>
identifier 'a' [LeadingSpace]Loc=<b.c:2:5>
equal '='Loc=<b.c:2:6>
numeric_constant '3'Loc=<b.c:2:7>
semi ';'Loc=<b.c:2:8>
b.c:3:1: error: unterminated /* comment
/* hello
^
eof ''Loc=<b.c:6:2>
1 error generated.
The number 4 is totally fine with lexer. you can see all the token identified by lexer. 09 is a numeric_constant.

~/Downloads$ clang -Xclang -dump-tokens 4.c
int 'int' [StartOfLine]Loc=<a.c:1:1>
identifier 'main' [LeadingSpace]Loc=<a.c:1:5>
l_paren '('Loc=<a.c:1:9>
r_paren ')'Loc=<a.c:1:10>
l_brace '{' [LeadingSpace]Loc=<a.c:1:12>
int 'int' [StartOfLine] [LeadingSpace]Loc=<a.c:2:2>
identifier 'a' [LeadingSpace]Loc=<a.c:2:6>
equal '=' [LeadingSpace]Loc=<a.c:2:8>
numeric_constant '09' [LeadingSpace]Loc=<a.c:2:10>
semi ';'Loc=<a.c:2:12>
return 'return' [StartOfLine] [LeadingSpace]Loc=<a.c:3:2>
numeric_constant '0' [LeadingSpace]Loc=<a.c:3:9>
semi ';'Loc=<a.c:3:10>
r_brace '}' [StartOfLine]Loc=<a.c:4:1>
eof ''Loc=<a.c:4:2>

 

1 votes
1 votes
Your answer is correct. There are two programs with Lexical errors. They are 1 (@ is not identified) and 3 (non ending comment is not identified). Rest are fine with the lexer.

Related questions

1 votes
1 votes
1 answer
1
aaru14 asked Nov 21, 2017
1,400 views
in c programming which of the following is not used as a token seprator during lexical analysis?a)white spaceb)commentc)semicolond)none of thesetoken seprator means??
4 votes
4 votes
2 answers
3
jatin khachane 1 asked Nov 23, 2018
1,208 views
int main() { int a,b; a=10; b=15; printf("a=%d,b=%d",a++,b ); }The number of tokens in the above C program is_________.I am getting 30The answer given is 29