[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bison string processing
From: |
Hans Aberg |
Subject: |
Re: Bison string processing |
Date: |
Wed, 29 Nov 2000 10:59:48 +0100 |
At 12:10 +0300 0-11-29, Vladimir Rykov wrote:
>I have to write a program which converts the input string:
>
> vv implies ww ;
>
>in the output line :
Bison parses lexemes, or "words", of a language, which are produced by the
yylex functions. A simple way to produce such lexemes is by using the a
lexical analyzer compiler like Flex, see
ftp://ftp.digital.com/pub/GNU/non-gnu/flex/flex-2.5.4a.tar.gz
which from a regular-word-like definition writes a definition of yylex.
In Flex one could write a definition say like
[[:alpha:]]+ { /* save (yytext, yyleng) say in yylval */ return NAME; }
"=>" { return IMPLIES; }
\n { ++line_number; return yytext[0]; }
. { return yytext[0]; /* Single character becomes code. */ }
In Bison, one would define
%token IMPLIES, NAME, ...
and use in variable definitions as
exp:
NAME { ... }
| exp IMPLIES exp { ... }
The Bison manual 3.6.1 also says that if you declare
%token IMPLIES "=>"
then you can later use that name as in
exp:
NAME { ... }
| exp "=>" exp { ... }
But here "=>" is just a symbol for IMPLIES, a way to make your Bison
definition file look nicer; yylex still needs to recognize the token
IMPLIES, which could be something else than "=>".
-- The Bison manual 4.2.1 indicates a way for yylex to use token table
lookups, but this is said to be slow.
Hans Aberg