aboutsummaryrefslogtreecommitdiff
path: root/gcc/treelang/lex.l
diff options
context:
space:
mode:
authorTim Josling <tej@melbpc.org.au>2002-05-05 04:24:18 +0000
committerTim Josling <timjosling@gcc.gnu.org>2002-05-05 04:24:18 +0000
commit6cfea11bd14394d177d61efd1f1cc908ce5534b2 (patch)
treef9f7f1ab980437234d5a7681a7427727d00adb41 /gcc/treelang/lex.l
parent6d030676cc896eeb605f7997f76942a7734b661b (diff)
downloadgcc-6cfea11bd14394d177d61efd1f1cc908ce5534b2.zip
gcc-6cfea11bd14394d177d61efd1f1cc908ce5534b2.tar.gz
gcc-6cfea11bd14394d177d61efd1f1cc908ce5534b2.tar.bz2
Added new sample language treelang.
From-SVN: r53169
Diffstat (limited to 'gcc/treelang/lex.l')
-rw-r--r--gcc/treelang/lex.l294
1 files changed, 294 insertions, 0 deletions
diff --git a/gcc/treelang/lex.l b/gcc/treelang/lex.l
new file mode 100644
index 0000000..97b06a6
--- /dev/null
+++ b/gcc/treelang/lex.l
@@ -0,0 +1,294 @@
+%{ /* -*- c -*- = mode for emacs editor
+/*
+
+ TREELANG lexical analysis
+
+ ---------------------------------------------------------------------
+
+ Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; either version 2, or (at your option) any
+ later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+
+ In other words, you are welcome to use, share and improve this program.
+ You are forbidden to forbid anyone else to use, share and improve
+ what you give them. Help stamp out software-hoarding!
+
+ ---------------------------------------------------------------------
+
+ Written by Tim Josling 1999-2001, based in part on other parts of
+ the GCC compiler.
+
+*/
+
+/* Avoid poisoned malloc problem. */
+#undef IN_GCC
+
+#if 0
+/* tree is defined as void* here to avoid any knowledge of tree stuff in this file. */
+typedef void *tree;
+#endif
+#include <stdio.h>
+#if 0
+#include <ctype.h>
+#endif
+#include <memory.h>
+#include "ansidecl.h"
+#include "config.h"
+#include "system.h"
+#include "diagnostic.h"
+
+/* Token defs. */
+#include "treelang.h"
+#include "parse.h"
+
+extern int option_lexer_trace;
+
+int yylex (void);
+void update_yylval (int a);
+
+static int next_tree_lineno=1;
+static int next_tree_charno=1;
+
+static void update_lineno_charno (void);
+static void dump_lex_value (int lexret);
+
+#define SAVE_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
+ {fprintf (stderr, "\nlexer returning"); dump_lex_value (a);} return a;}
+#define NOT_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
+ {fprintf (stderr, "\nlexer swallowing"); dump_lex_value (a);}}
+
+%}
+
+%option nostack
+%option nounput
+%option noyywrap
+%option pointer
+%option nodefault
+
+%%
+
+ {
+ yylval = my_malloc (sizeof (struct token));
+ ((struct token*)yylval)->lineno = next_tree_lineno;
+ ((struct token*)yylval)->charno = next_tree_charno;
+ }
+
+[ \n]+ {
+ update_lineno_charno ();
+ NOT_RETURN (WHITESPACE);
+}
+
+"//".* {
+ /* Comment. */
+ update_lineno_charno ();
+ NOT_RETURN (COMMENT);
+}
+
+"{" {
+ update_lineno_charno ();
+ SAVE_RETURN (LEFT_BRACE);
+}
+
+"}" {
+ update_lineno_charno ();
+ SAVE_RETURN (RIGHT_BRACE);
+}
+
+"(" {
+ update_lineno_charno ();
+ SAVE_RETURN (LEFT_PARENTHESIS);
+}
+
+")" {
+ update_lineno_charno ();
+ SAVE_RETURN (RIGHT_PARENTHESIS);
+}
+
+"," {
+ update_lineno_charno ();
+ SAVE_RETURN (COMMA);
+}
+
+";" {
+ update_lineno_charno ();
+ SAVE_RETURN (SEMICOLON);
+}
+
+"+" {
+ update_lineno_charno ();
+ SAVE_RETURN (PLUS);
+}
+
+"-" {
+ update_lineno_charno ();
+ SAVE_RETURN (MINUS);
+}
+
+"=" {
+ update_lineno_charno ();
+ SAVE_RETURN (ASSIGN);
+}
+
+"==" {
+ update_lineno_charno ();
+ SAVE_RETURN (EQUALS);
+}
+
+[+-]?[0-9]+ {
+ update_lineno_charno ();
+ SAVE_RETURN (INTEGER);
+}
+
+"external_reference" {
+ update_lineno_charno ();
+ SAVE_RETURN (EXTERNAL_REFERENCE);
+}
+
+"external_definition" {
+ update_lineno_charno ();
+ SAVE_RETURN (EXTERNAL_DEFINITION);
+}
+
+"static" {
+ update_lineno_charno ();
+ SAVE_RETURN (STATIC);
+}
+
+"automatic" {
+ update_lineno_charno ();
+ SAVE_RETURN (STATIC);
+}
+
+"int" {
+ update_lineno_charno ();
+ SAVE_RETURN (INT);
+}
+
+"char" {
+ update_lineno_charno ();
+ SAVE_RETURN (CHAR);
+}
+
+"void" {
+ update_lineno_charno ();
+ SAVE_RETURN (VOID);
+}
+
+"unsigned" {
+ update_lineno_charno ();
+ SAVE_RETURN (UNSIGNED);
+}
+
+"return" {
+ update_lineno_charno ();
+ SAVE_RETURN (RETURN);
+}
+
+"if" {
+ update_lineno_charno ();
+ SAVE_RETURN (IF);
+}
+
+"else" {
+ update_lineno_charno ();
+ SAVE_RETURN (ELSE);
+}
+
+[A-Za-z_]+[A-Za-z_0-9]* {
+ update_lineno_charno ();
+ update_yylval (NAME);
+ if (option_lexer_trace)
+ {
+ fprintf (stderr, "\nlexer returning");
+ dump_lex_value (NAME);
+ }
+ return NAME;
+}
+
+[^\n] {
+ update_lineno_charno ();
+ fprintf (stderr, "%s:%i:%i: Unrecognized character %c\n", in_fname,
+ ((struct token*)yylval)->lineno,
+ ((struct token*)yylval)->charno, yytext[0]);
+ errorcount++;
+}
+
+%%
+
+/*
+ Update line number (1-) and character number (1-). Call this
+ before processing the token. */
+
+static void
+update_lineno_charno (void)
+{
+ /* Update the values we send to caller in case we sometimes don't
+ tell them about all the 'tokens' eg comments etc. */
+ int yyl;
+ ((struct token*)yylval)->lineno = next_tree_lineno;
+ ((struct token*)yylval)->charno = next_tree_charno;
+ for ( yyl = 0; yyl < yyleng; ++yyl )
+ {
+ if ( yytext[yyl] == '\n' )
+ {
+ ++next_tree_lineno;
+ next_tree_charno = 1;
+ }
+ else
+ next_tree_charno++;
+ }
+}
+
+/* Fill in the fields of yylval - the value of the token. The token
+ type is A. */
+void
+update_yylval (int a)
+{
+ struct token* tok;
+ tok=yylval;
+
+ tok->category = token_category;
+ tok->type = a;
+ tok->length = yyleng;
+ /* Have to copy yytext as it is just a ptr into the buffer at the
+ moment. */
+ tok->chars = my_malloc (yyleng + 1);
+ memcpy (tok->chars, yytext, yyleng);
+}
+
+/* Trace the value LEXRET and the position and token details being
+ returned by the lexical analyser. */
+
+static void
+dump_lex_value (int lexret)
+{
+ int ix;
+ fprintf (stderr, " %d l:%d c:%d ln:%d text=", lexret,
+ ((struct token*) yylval)->lineno,
+ ((struct token*) yylval)->charno,
+ ((struct token*) yylval)->length);
+ for (ix = 0; ix < yyleng; ix++)
+ {
+ fprintf (stderr, "%c", yytext[ix]);
+ }
+ fprintf (stderr, " in hex:");
+ for (ix = 0; ix < yyleng; ix++)
+ {
+ fprintf (stderr, " %2.2x", yytext[ix]);
+ }
+ fprintf (stderr, "\n");
+}
+