The Gateway to Computer Science Excellence
+3 votes
239 views

in Compiler Design by Active (3.5k points)
edited by | 239 views
0
only program 3?

3 Answers

+1 vote
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>

 

by Junior (657 points)
0 votes
P1,  P3 and P4
by (223 points)
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
0 votes
prog 1 and 3
by (39 points)
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,615 answers
195,894 comments
102,331 users