c - Flex And Bison, detecting macro statements (newbie) -
i want teach flex & bison detect macro definitions in pure c. i'am adding function existing parser form here. parser good, lacks macro functionality. did add #include
, pragma
macros detection, selection macroses have problems, code in parser:
macro_selection_variants : macro_compound_statement | include_statement | pragma_statement | macro_selection_statement | statement ; macro_selection_statement : macro_ifdef identifier macro_selection_variants macro_endif | macro_ifdef identifier macro_selection_variants macro_else macro_selection_variants macro_endif | macro_ifndef identifier macro_selection_variants macro_endif | macro_ifndef identifier macro_selection_variants macro_else macro_selection_variants macro_endif ;
statement
declared so:
statement : labeled_statement | compound_statement | expression_statement | selection_statement | iteration_statement | jump_statement ;
and lexer part macroses is:
"#ifdef" { count(); return(macro_ifdef); } "#ifndef" { count(); return(macro_ifndef); } "#else" { count(); return(macro_else); } "#endif" { count(); return(macro_endif); }
so problem 2 reduce/reduce
errors because i'm trying use statement
in macro_selection_statement
. need use statement
in macro selection block, because blocks can have variables definitions likes so:
#ifdef user #include "user.h" int some_var; char some_text[]="hello"; #ifdef 1 int two=0; #endif #endif
what right move here? because read %expect -rr n
bad thing reduce
warnings.
you cannot expect implement preprocessor (properly) inside of c grammar. needs *pre*processor; is, reads program text, , output sent c grammar.
it possible (mostly) avoid doing second lex pass, since (in theory) preprocessor can output tokens rather stream of characters. work bison 2.7-or-better "push parser", might want give try. traditional approach stream of characters, may easier.
it's important remember replacement text of macro, arguments macro, have no syntactic constraints. (or no constraints.) following entirely legal:
#define open { #define close } #define say(whatever) puts(#whatever); #include <stdio.h> int main(int argc, char** argv) open say(===>) return 0; close
and that's start :)
Comments
Post a Comment