positional parameter in lex and yacc !!

positional parameter in lex and yacc !!

Post by Baij » Fri, 14 Nov 1997 04:00:00



Hi,

Again a problem with yacc !!!
:) How do we get the value of string type positional arguments from a
yacc grammar. Insted of explaining in detail I will attach the complete
code(not so big)
along with this.

In tp.y, which is a yacc file, I am trying to get the value of a string
type token.
The same positional parameter is working for integer type of tokens.

I don't think that positional parameters are only for integer type of
tokens, by logic.
If so, how do we get the value of string or other type of (eg: a
userdefined class) variables ?

One more thing before closing, Is there any dos/windows version of lex
and yacc (flex & bison)
compiler utilities which will generate C++ codes ?

I really appreciate , if you can throw some light on this.

Thanks
Baiju

------------tp.l---(not too big file)---------------

digit [0-9]
digits {digit}+
whitespace [\t]
Space [ ]+
String ([A-Z]*[a-z]*)+

%%
[(]  {printf("%s\n", yytext);  return('(');}
[)]   {printf("%s\n", yytext); return(')');}
[=]  {printf("%s\n", yytext); return('=');}
["]  {printf("%s\n", yytext); return('\"');}
[,]  {printf("%s\n", yytext); return(',');}

{digits} {
            yylval.Integer = atoi(yytext);
     printf("%d\n", yylval.Integer);
     return(NUMBER);
         }

\n   { printf("%s\n", yytext); return('\n'); }

{whitespace} ;

"TO"  {printf("%s\n", yytext); return(TO);}

"SendMessage"            {printf("%s\n", yytext); return(SENDMESSAGE);}

{String} {printf("From Lexer %s\n",yytext); return(STRING);}

{Space}  {return(SPACE);}

.   {printf("Got a Bad Token [%s]\n!!", yytext); return(BADTOKEN);}

(quit)|(exit)   return 0;

------------tp.y------------------------------------
%{
  int LineNumber = 1;
  char TmpString[100];
%}

%union {
int Integer;
char Array[100];

Quote:}

%start StartSymbol

%token TO SPACE
%token <Integer> NUMBER
%token <Array> STRING
%token BADTOKEN

%token SENDMESSAGE

%%
StartSymbol  :
  | StartSymbol ValidExpr '\n'
    {
                        LineNumber = LineNumber + 1;
   printf("Valid Expression \n");
    }
  ;

ValidExpr : LhsTO '=' RhsTO

LhsTO         : TO NUMBER ;

RhsTO  : TransactionName ;

TransactionName : SendMessageXN ;

/*----------SendMessage()----------------------------------*/

SendMessageXN  : SENDMESSAGE '(' SendMessageParam ')' ;
SendMessageParam : NUMBER ',' STRING
     {
      printf("FromParserIntegerVal = [%d]\n", $1);
   printf("FromParserStringVal  = [%s]\n", $3);
     };

%%
#include "lex.yy.c"
main()
{
  return yyparse();

Quote:}

yyerror(char* s)
{
  fprintf(stderr,"ERROR: [%s] at Line Number: [%d] of the script
file\n",s,LineNumber);

Quote:}

------------test----file--------------------------

TO3=SendMessage(144,MSMFC)

-------------compilation step and o/p---------------------
baiju/samples>
baiju/samples> lex tp.l
baiju/samples> yacc tp.y
baiju/samples> cc -o tp y.tab.c -ll
baiju/samples> tp < test
TO
3
=
SendMessage
(
144
,
From Lexer MSMFC
FromParserIntegerVal = [144]
FromParserStringVal  = []
)

Valid Expression
baiju/samples>

-------------expected output !!!-----------------
TO
3
=
SendMessage
(
144
,
From Lexer MSMFC
FromParserIntegerVal = [144]
FromParserStringVal  = [MSMFC]
)

Valid Expression
-------------------------------------------------

 
 
 

positional parameter in lex and yacc !!

Post by Glenn Wes » Sat, 15 Nov 1997 04:00:00



> Hi,

> Again a problem with yacc !!!
> :) How do we get the value of string type positional arguments from a
> yacc grammar. Insted of explaining in detail I will attach the complete
> code(not so big)
> along with this.

[code snipped]

This isn't really a shell question, is it?  See comp.unix.programmer for
the solution...