help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

LaTeX \frac parsing


From: Alin-Florin Rus-Rebreanu
Subject: LaTeX \frac parsing
Date: Tue, 13 Oct 2009 01:56:06 +0300

Hi,

I'm trying to parse an expression made up of various symbols and the
\frac{}{} operator from LaTeX, but my grammar fails miserably since
I'm new with bison.

Flex
------

"\\"[Ff]rac             { return FUNC; }
"^"                     { return POW; }

"{"                     { return OP; }
"}"                     { return CP; }

[0-9]+                  { yylval.ival = atoi(yytext); return NUMBER; }
m|ml|mp|dsa     { yylval.strval = strdup(yytext); return ISU; }
\n                      { return EOL; }
[ \t]                   { }
.                       { }


bison
--------

%union {
        struct ast *a;
        char *strval;
        int ival;       
}
%debug
%type <a> exp fact
%token <strval> ISU
%token <ival> NUMBER
%token FUNC POW
%token EOL OP CP

%nonassoc POW

%%

calclist:
        | calclist factor EOL {
         printf("result >",eval($2));
        }
;


exp: fact
        | exp fact {$$ = newast('*',$1,$2)};

fact:  ISU {$$ = newnum($1,1);}
     | ISU POW NUMBER {$$ = newnum($1,$3);}
             | FUNC OP factor CP OP factor CP { $$ = newast('/',$3,$6);};


The idea is that I evaluate such expressions symbolically. There's no
problem with the AST implementation, only the grammar remains an
issue. It fails even for simple cases

\frac{m}{mp}
nodetype /
nodetype K
mp^1
nodetype K
mp^1

I should pe able to manipulate product of fractions such as
\frac{m]{mp}\frac{mp}{m}
Or even \frac{\frac{m}{mp}}{m}. I tried various grammars but I really
can't get my head arround it. Any pointers ? What am I missing?

Thank you




reply via email to

[Prev in Thread] Current Thread [Next in Thread]