By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

yacc trouble

P: n/a
Hi Guys,

I am facing a peculiar problem with my yacc script. Following is the
snippet of the yacc script that i am using.
I am using lex as the lexical analyzer.
Sample Input :
create template {
with attributes :
attr1 ;
attr2 ;
attr3 ;
}
tpl_name

Output that i get should be ideally
Attribute name is attr1
Attribute name is attr2
Attribute name is attr3

But what i get is

Attribute name is attr1 ;
Attribute name is attr2 ;
Attribute name is attr3 ;

The semicolon appears at the end. Does someone know why this
happens? Would be glad to furnish more information if needed.
The lex seems to be passing the correct token.

Thanks in advance
Mohit

----------------------------------------------------------------------------------------------------------------------
The Yacc Script Snippet
----------------------------------------------------------------------------------------------------------------------

start : CREATE TEMPLATE '{' create_template_body '}'
IDENTIFIER
{
parsedTemplate =
appendTemplateName(strdup($6),$4);
}
;
create_template_body : define_attributes
{
$$ = createTemplate($1);
}
;

define_attributes : WITH ATTRIBUTES ':' attribute_list
{
$$ = $4;
}
;
attribute_list : attribute_list attribute
{
$$ = appendAttribute($1,$2);
// Append attribute to Attribute List
}
| attribute
{
$$ = createAttributeList($1);
}
;
attribute : IDENTIFIER ';'
{
printf("Attribute Name is %s\n",
strdup($1));
$$ = createAttribute(strdup($1));
}
---------------------------------------------------------------------------------------------------------------------
The Lex Script Snippet
---------------------------------------------------------------------------------------------------------------------

PS: There are some other tokens here which might not make sense
because i pasted only the relevant parts of the yacc file.

digit [0-9]
letter [a-zA-Z_.]
word {letter}({letter}|{digit})*
ws [ \n\t]
string \"[^"\n]*["\n]
number {digit}+
%%
"," { showToken(",",yytext); return ','; }
":" { showToken(":",yytext); return ':'; }
"{" { showToken("{",yytext); return '{'; }
"}" { showToken("}",yytext); return '}'; }
";" { showToken(";",yytext); return ';'; }
{string} { yylval.str = strdup(yytext+1);
if (yylval.str[yyleng-2] != '"')
reportError("improperly terminated string");
else {
yylval.str[yyleng-2] = 0;
showToken("STRING", yytext);
return STRING;
}
}
{word} {
if (isIdentifier(yytext) == IDENTIFIER)
{
yylval.str = yytext;
showToken("IDENTIFIER",yytext);
return isIdentifier(yytext);
}
else
{
yylval.str = yytext;
showToken("RESERVED", yytext);
return isIdentifier(yytext);
}
}
{number} {
yylval.num = atoi(yytext);
showToken("NUMBER",yytext);
return NUMBER;
}
{ws} ; /* ignore */
{comment} ; /* ignore */
.. {
printf("bad char: '%c'\n", yytext[0]);
showToken("******************",yytext);
}
%%

Jun 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mohitz <co********@gmail.comwrites:
I am facing a peculiar problem with my yacc script. Following is the
snippet of the yacc script that i am using.
I am using lex as the lexical analyzer.
Sample Input :
create template {
with attributes :
attr1 ;
attr2 ;
attr3 ;
}
tpl_name

Output that i get should be ideally
Attribute name is attr1
Attribute name is attr2
Attribute name is attr3

But what i get is

Attribute name is attr1 ;
Attribute name is attr2 ;
Attribute name is attr3 ;

The semicolon appears at the end. Does someone know why this
happens? Would be glad to furnish more information if needed.
The lex seems to be passing the correct token.
[snip]

This isn't a question about C. You might try comp.compilers.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 4 '07 #2

P: n/a
ok thanks i will..
On Jun 4, 11:47 am, Keith Thompson <k...@mib.orgwrote:
Mohitz <coolmoh...@gmail.comwrites:
I am facing a peculiar problem with my yacc script. Following is the
snippet of the yacc script that i am using.
I am using lex as the lexical analyzer.
Sample Input :
create template {
with attributes :
attr1 ;
attr2 ;
attr3 ;
}
tpl_name
Output that i get should be ideally
Attribute name is attr1
Attribute name is attr2
Attribute name is attr3
But what i get is
Attribute name is attr1 ;
Attribute name is attr2 ;
Attribute name is attr3 ;
The semicolon appears at the end. Does someone know why this
happens? Would be glad to furnish more information if needed.
The lex seems to be passing the correct token.

[snip]

This isn't a question about C. You might try comp.compilers.

--
Keith Thompson (The_Other_Keith) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"- Hide quoted text -

- Show quoted text -

Jun 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.