Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
####### # # E-scripts on Lex/Flex and Yacc/Bison. # # Note 1: use the eev command (defined in eev.el) and the # ee alias (in my .zshrc) to execute parts of this file. # Executing this file as a whole makes no sense. # An introduction to eev can be found here: # # (find-eev-quick-intro) # http://angg.twu.net/eev-intros/find-eev-quick-intro.html # # Note 2: be VERY careful and make sure you understand what # you're doing. # # Note 3: If you use a shell other than zsh things like |& # and the for loops may not work. # # Note 4: I always run as root. # # Note 5: some parts are too old and don't work anymore. Some # never worked. # # Note 6: the definitions for the find-xxxfile commands are on my # .emacs. # # Note 7: if you see a strange command check my .zshrc -- it may # be defined there as a function or an alias. # # Note 8: the sections without dates are always older than the # sections with dates. # # This file is at <http://angg.twu.net/e/lexyacc.e> # or at <http://angg.twu.net/e/lexyacc.e.html>. # See also <http://angg.twu.net/emacs.html>, # <http://angg.twu.net/.emacs[.html]>, # <http://angg.twu.net/.zshrc[.html]>, # <http://angg.twu.net/escripts.html>, # and <http://angg.twu.net/>. # ####### # «.flex» (to "flex") # «.eeflex» (to "eeflex") # «.bison» (to "bison") # «.bison_calcs» (to "bison_calcs") # «.precedence» (to "precedence") # (find-htetfile "Lex-YACC-HOWTO.gz") ##### # # flex # 2001jan21 # ##### # «flex» (to ".flex") # (find-status "flex") # (find-vldifile "flex.list") # (find-fline "/usr/doc/flex/") # (find-fline "/usr/doc/flex/NEWS.gz" "start condition scope") # (find-node "(flex)Simple Examples") # (find-node "(flex)Format") # (find-node "(flex)Definitions Section" "_indented_") # (find-node "(flex)Rules Section") # (find-node "(flex)User Code Section") # (find-node "(flex)Options" "including `%option' directives") # (find-node "(flex)Generated scanner" "your own version of `yywrap()'") #* zcatinfo /usr/info/flex > /tmp/flex.info zcatinfo /usr/share/info/bison > /tmp/bison.info # (find-fline "/tmp/flex.info") # (find-fline "/tmp/bison.info") #* rm -Rv /tmp/lex/ mkdir /tmp/lex/ cd /tmp/lex/ cat > wc.lex <<'---' %option noyywrap int num_lines = 0, num_chars = 0; %% \n ++num_lines; ++num_chars; . ++num_chars; %% main() { yylex(); printf("%7d lines, %d chars\n", num_lines, num_chars); } --- flex wc.lex gcc lex.yy.c ./a.out < wc.lex wc wc.lex # (find-fline "/tmp/lex/") # (find-fline "/tmp/lex/lex.yy.c") #* ##### # # eeflex # 2004nov06 # ##### # «eeflex» (to ".eeflex") #* rm -Rv /tmp/lex/ mkdir /tmp/lex/ cd /tmp/lex/ eeflex -g <<'---' %option noyywrap int num_lines = 0, num_chars = 0; %% \n ++num_lines; ++num_chars; . ++num_chars; %% main() { yylex(); printf("%7d lines, %d chars\n", num_lines, num_chars); } --- eec < $EEVTMPDIR/ee.l wc $EEVTMPDIR/ee.l #* ##### # # bison # 2001jan20 # ##### # «bison» (to ".bison") # (find-status "bison") # (find-vldifile "bison.list") # (find-fline "/usr/share/doc/bison/") #* pdsc $SDEBIAN/dists/potato/main/source/devel/bison_1.28-5.dsc cd /usr/src/bison-1.28/ #* # (code-c-d "bison" "/usr/src/bison-1.28/") # (find-bisonfile "") ##### # # bison demos: rpcalc and calc # 2001jan20 # ##### # «bison_calcs» (to ".bison_calcs") # (find-node "(bison)Infix Calc") * (eepitch-shell) * (eepitch-kill) * (eepitch-shell) #* rm -Rv /tmp/bison/ mkdir /tmp/bison/ cd /tmp/bison/ cat > rpcalc.y- <<'---' %{ #define YYSTYPE double #include <math.h> %} %token NUM %% /* Grammar rules and actions follow */ input: /* empty */ | input line ; line: '\n' | exp '\n' { printf ("\t%.10g\n", $1); } ; exp: NUM { $$ = $1; } | exp exp '+' { $$ = $1 + $2; } | exp exp '-' { $$ = $1 - $2; } | exp exp '*' { $$ = $1 * $2; } | exp exp '/' { $$ = $1 / $2; } /* Exponentiation */ | exp exp '^' { $$ = pow ($1, $2); } /* Unary minus */ | exp 'n' { $$ = -$1; } ; %% --- cat > calc.y- <<'---' %{ #define YYSTYPE double #include <math.h> %} /* BISON Declarations */ %token NUM %left '-' '+' %left '*' '/' %left NEG /* negation--unary minus */ %right '^' /* exponentiation */ /* Grammar follows */ %% input: /* empty string */ | input line ; line: '\n' | exp '\n' { printf ("\t%.10g\n", $1); } | error '\n' { yyerrok; } ; exp: NUM { $$ = $1; } | exp '+' exp { $$ = $1 + $3; } | exp '-' exp { $$ = $1 - $3; } | exp '*' exp { $$ = $1 * $3; } | exp '/' exp { $$ = $1 / $3; } | '-' exp %prec NEG { $$ = -$2; } | exp '^' exp { $$ = pow ($1, $3); } | '(' exp ')' { $$ = $2; } ; %% --- cat > calc_common.y- <<'---' /* Lexical analyzer returns a double floating point number on the stack and the token NUM, or the ASCII character read if not a number. Skips all blanks and tabs, returns 0 for EOF. */ #include <ctype.h> #include <stdio.h> yylex () { int c; /* skip white space */ while ((c = getchar ()) == ' ' || c == '\t') ; /* process numbers */ if (c == '.' || isdigit (c)) { ungetc (c, stdin); scanf ("%lf", &yylval); return NUM; } /* return end-of-file */ if (c == EOF) return 0; /* return single chars */ return c; } main () { yyparse (); } #include <stdio.h> /* Called by yyparse on error: */ yyerror (char *s) { printf ("%s\n", s); } --- (cat rpcalc.y-; echo; cat calc_common.y-) > rpcalc.y (cat calc.y-; echo; cat calc_common.y-) > calc.y bison rpcalc.y gcc rpcalc.tab.c -lm -o rpcalc bison calc.y gcc calc.tab.c -lm -o calc ./rpcalc <<'---' 4 9 + 3 7 + 3 4 5 *+- 3 7 + 3 4 5 * + - n 5 6 / 4 n + 3 4 ^ --- ./calc <<'---' 4 + 4.5 - (34/(8*3+-3)) -56 + 2 3 ^ 2 --- #* # (find-fline "/tmp/bison/") # (find-node "(bison)") # (find-bisonnode "Rpcalc Declarations") # (find-bisonnode "Rpcalc Generate") ##### # # Precedence # 2011dec24 # ##### # «precedence» (to ".precedence") # (find-es "ml" "levy") # (find-bisonnode "Precedence Decl") # (find-status "bison") # (find-vldifile "bison.list") # (find-udfile "bison/") # (find-status "bison-doc") # (find-vldifile "bison-doc.list") # (find-udfile "bison-doc/") # (find-status "flex") # (find-vldifile "flex.list") # (find-udfile "flex/") # (find-status "flex-doc") # (find-vldifile "flex-doc.list") # (find-udfile "flex-doc/") # (find-node "(bison)Algorithm" "shifting") # (find-node "(bison)Algorithm" "reduced") # (find-node "(bison)Lookahead" "!") # Local Variables: # coding: utf-8-unix # End: