]> git.saurik.com Git - bison.git/blobdiff - examples/calc++/calc++-scanner.ll
Start a set of simple examples.
[bison.git] / examples / calc++ / calc++-scanner.ll
diff --git a/examples/calc++/calc++-scanner.ll b/examples/calc++/calc++-scanner.ll
new file mode 100644 (file)
index 0000000..dd9a8b8
--- /dev/null
@@ -0,0 +1,45 @@
+%{                                                            /* -*- C++ -*- */
+
+# include <string>
+# include <cerrno>
+# include "calc++-driver.hh"
+# include "calc++-parser.hh"
+%}
+
+%option noyywrap nounput debug batch
+
+id    [a-zA-Z][a-zA-Z_0-9]*
+int   [0-9]+
+blank [ \t]
+
+%%
+
+%{
+# define YY_USER_ACTION  yylloc->columns (yyleng);
+  yylloc->step ();
+%}
+{blank}+   yylloc->step ();
+[\n]+      yylloc->lines (yyleng); yylloc->step ();
+
+
+[-+*/]     return yytext[0];
+":="       return TOKEN_ASSIGN;
+{int}      yylval->ival = atoi (yytext); return TOKEN_NUMBER;
+{id}       yylval->sval = new std::string (yytext); return TOKEN_IDENTIFIER;
+.          driver.error (*yylloc, "invalid character");
+
+%%
+
+void
+calcxx_driver::scan_begin ()
+{
+  yy_flex_debug = trace_scanning;
+  if (!(yyin = fopen (file.c_str (), "r")))
+    error (std::string ("cannot open ") + file);
+}
+
+void
+calcxx_driver::scan_end ()
+{
+  fclose (yyin);
+}