search
Log In
3 votes
444 views

in Compiler Design
edited by
444 views
0
only program 3?

3 Answers

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

 

1 vote
prog 1 and 3
0 votes
P1,  P3 and P4
0
why n't p2?...and why p4?
0
There is a syntax error in P2 but there is nothing in P2 which can not be identified by lexical analyser. And in P4 09 in out of the range of octal code but lex can identify only octal numbers hence error

Related questions

1 vote
1 answer
1
170 views
in c programming which of the following is not used as a token seprator during lexical analysis? a)white space b)comment c)semicolon d)none of these token seprator means??
asked Nov 22, 2017 in Compiler Design aaru14 170 views
2 votes
1 answer
3
222 views
Given Solution: It is given that Program 4 will cause Lexical error as ..09 is octal and assigned to int -- But Lexical analyzer just treat 09 as constant ..So program 4 will give Semantic error than lexical Correct me if i am wrong
asked Nov 23, 2018 in Compiler Design jatin khachane 1 222 views
...