| Newsgroups |
gmane.comp.parsers.bison.general |
| Message-ID |
<[email protected]> |
Now a very simple question: i have this lexer.l file:
%{
#include <stdio.h>
#include <stdlib.h>
enum yytokentype {
NUMBER = 285,
VAL = 292,
ADD = 286,
SUB = 287,
MUL = 288,
DIV = 289,
ABS = 290,
EOL = 291,
STRING = 292
};
int yylval;
char yyltext[256];
%}
%%
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"|" { return ABS; }
[a-zA-Z] { strcpy(yytext, yyltext); return STRING; }
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
\n { return EOL; }
[ \t] { /* ignore whitespaces */ }
. { printf("Unknow char :%c", *yytext); }
%%
int main(int argc, char **argv){
int tok;
while(tok = yylex())
{
printf("%d", tok);
if(tok == STRING) {
printf(" = %s\n", &yyltext);
}else if(tok == NUMBER) {
printf(" = %d\n", yylval);
}else printf("\n");
}
return(0);
}
what am i doing wrong so that i'm not able to parse strings with [a-zA-Z] ?
On 17.02.19 15:53, Akim Demaille wrote:
>
>>> Le 17 févr. 2019 à 14:17, [email protected] <[email protected]> a écrit :
>>>
>>> Is there a way i can put my c source code not inside one the the lexer.l or parser.y files ? so i can keep tem separate from the rules ?
> Two opposite answers:
>
> I said:
>
>> Le 17 févr. 2019 à 15:49, Akim Demaille <[email protected]> a écrit :
>>
>> No, sorry. There are several approaches to parsing, one which is fully declarative and your rules are "pure". That's not the case of Flex/Bison: you must define rules with actions. Yet you should keep your action simple and move complex processing into functions.
> Uxio said:
>
>> Le 17 févr. 2019 à 15:46, Uxio Prego <[email protected]> a écrit :
>>
>> Yes of course, by inclusion of headers, in a very much
>> common way. You can then manipulate shorter *.y and
>> *.l docs, but this is not going to fix any Bison usage issue
>> you are having.
> And of course Uxio is right. You can put the function you depend upon in other compilation units (i.e., other *.c files). What I meant is: Flex and Bison are useless if you don't *call* code from your actions.
_______________________________________________
[email protected] https://lists.gnu.org/mailman/listinfo/help-bison