1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

Convert over to the new way of handling lex/bison checked into cvs

llvm-svn: 26209
This commit is contained in:
Chris Lattner 2006-02-15 07:26:07 +00:00
parent 0974ebab7b
commit 0ebae20ee1
5 changed files with 419 additions and 0 deletions

View File

@ -0,0 +1,234 @@
/*===-- Lexer.l - Scanner for Stacker language -----------------*- C++ -*--===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and donated to the LLVM research
// group and is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the flex scanner for Stacker languages files.
//
//===----------------------------------------------------------------------===*/
%option prefix="Stacker"
%option yylineno
%option nostdinit
%option never-interactive
%option batch
%option noyywrap
%option nodefault
%option 8bit
%option outfile="Lexer.cpp"
%option ecs
%option noreject
%option noyymore
%{
#include "StackerCompiler.h"
#include "StackerParser.h"
/* Conversion of text ints to binary */
static int64_t IntToVal(const char *Buffer) {
int64_t Result = 0;
for (; *Buffer; Buffer++) {
int64_t OldRes = Result;
Result *= 10;
Result += *Buffer-'0';
if (Result < OldRes) // Uh, oh, overflow detected!!!
StackerCompiler::ThrowException("constant bigger than 64 bits detected!");
}
return Result;
}
/* Conversion of text hexadecimal ints to binary */
static int64_t HexIntToVal(const char *Buffer) {
int64_t Result = 0;
for (; *Buffer; ++Buffer) {
int64_t OldRes = Result;
Result *= 16;
char C = *Buffer;
if (C >= '0' && C <= '9')
Result += C-'0';
else if (C >= 'A' && C <= 'F')
Result += C-'A'+10;
else if (C >= 'a' && C <= 'f')
Result += C-'a'+10;
if (Result < OldRes) // Uh, oh, overflow detected!!!
StackerCompiler::ThrowException("constant bigger than 64 bits detected!");
}
return Result;
}
#define YY_NEVER_INTERACTIVE 1
%}
/* Comments start with a ; and go till end of line */
Comment1 [#].*$
/* You can also embed them in ( ... ) */
Comment2 \(.*\)
/* We ignore white space */
White [ \t\r\n]
/* jdentifiers start with a % sign */
Identifier [A-Za-z][-A-Za-z0-9_]*
/* Strings can contain any character except " and \ */
String \"[^\"]*\"
/* Positive and negative integer constants*/
PInteger [+]?[0-9]+
NInteger -[0-9]+
HexInteger 0x[0-9A-Fa-f]+
/* Special Characters - name them to avoid flex confusion */
Semi [;]
Colon [:]
Less \<
More \>
LessEq \<\=
MoreEq \>\=
NotEq \<\>
Equal \=
Plus \+
Minus \-
Incr \+\+
Decr \-\-
Mult \*
Div \/
StarSlash \*\/
LShift \<\<
RShift \>\>
InStr \<s
InNum \<d
InChar \<c
OutStr \>s
OutNum \>d
OutChar \>c
%%
{Comment1} { /* Ignore comments */ }
{Comment2} { /* Ignore comments */ }
{Colon} { return COLON; }
{Semi} { return SEMI; }
TRUE { return TRUETOK; }
FALSE { return FALSETOK; }
ON { return TRUETOK; }
OFF { return FALSETOK; }
{Less} { return LESS; }
LT { return LESS; }
{More} { return MORE; }
GT { return MORE; }
{LessEq} { return LESS_EQUAL; }
LE { return LESS_EQUAL; }
{MoreEq} { return MORE_EQUAL; }
GE { return MORE_EQUAL; }
{NotEq} { return NOT_EQUAL; }
NE { return NOT_EQUAL; }
{Equal} { return EQUAL; }
EQ { return EQUAL; }
{Plus} { return PLUS; }
{Minus} { return MINUS; }
{Incr} { return INCR; }
{Decr} { return DECR; }
{Mult} { return MULT; }
{Div} { return DIV; }
MOD { return MODULUS; }
NEG { return NEGATE; }
ABS { return ABS; }
MIN { return MIN; }
MAX { return MAX; }
{StarSlash} { return STAR_SLASH; }
AND { return AND; }
OR { return OR; }
XOR { return XOR; }
{LShift} { return LSHIFT; }
{RShift} { return RSHIFT; }
DROP { return DROP; }
NIP { return NIP; }
DUP { return DUP; }
SWAP { return SWAP; }
OVER { return OVER; }
PICK { return PICK; }
SELECT { return SELECT; }
ROT { return ROT; }
RROT { return RROT; }
ROLL { return ROLL; }
TUCK { return TUCK; }
DROP2 { return DROP2; }
NIP2 { return NIP2; }
DUP2 { return DUP2; }
SWAP2 { return SWAP2; }
OVER2 { return OVER2; }
TUCK2 { return TUCK2; }
ROT2 { return ROT2; }
RROT2 { return RROT2; }
MALLOC { return MALLOC; }
FREE { return FREE; }
GET { return GET; }
PUT { return PUT; }
IF { return IF; }
ELSE { return ELSE; }
ENDIF { return ENDIF; }
WHILE { return WHILE; }
END { return END; }
RECURSE { return RECURSE; }
RETURN { return RETURN; }
EXIT { return EXIT; }
FORWARD { return FORWARD; }
TAB { return TAB; }
SPACE { return SPACE; }
CR { return CR; }
{InStr} { return IN_STR; }
{InNum} { return IN_NUM; }
{InChar} { return IN_CHAR; }
{OutStr} { return OUT_STR; }
{OutNum} { return OUT_NUM; }
{OutChar} { return OUT_CHAR; }
MAIN { return MAIN; }
DUMP { return DUMP; }
!= { StackerCompiler::ThrowException(
"You probably meant to use a <> instead of !=" ); }
== { StackerCompiler::ThrowException(
"You probably meant to use a single = .. this isn't C"); }
{PInteger} { Stackerlval.IntegerVal = IntToVal(yytext); return INTEGER; }
{NInteger} { uint64_t Val = IntToVal(yytext+1);
// +1: we have bigger negative range
if (Val > (uint64_t)INT64_MAX+1)
StackerCompiler::ThrowException(
"Constant too large for signed 64 bits!");
Stackerlval.IntegerVal = -Val;
return INTEGER;
}
{HexInteger} { Stackerlval.IntegerVal = HexIntToVal(yytext+3);
return INTEGER;
}
{String} { yytext[strlen(yytext)-1] = 0; // nuke end quote
Stackerlval.StringVal = strdup(yytext+1); // Nuke start quote
return STRING;
}
{Identifier} { Stackerlval.StringVal = strdup(yytext); return IDENTIFIER; }
{White} { /* Ignore whitespace */ }
%%

View File

@ -0,0 +1,185 @@
//===-- StackerParser.y - Parser for Stacker programs -----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the bison parser for Stacker programs.
//
//===----------------------------------------------------------------------===//
%{
#include "StackerCompiler.h"
#include "llvm/SymbolTable.h"
#include "llvm/Module.h"
#include "llvm/Instructions.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include <list>
#include <utility>
#include <algorithm>
#define YYERROR_VERBOSE 1
#define SCI StackerCompiler::TheInstance
int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
int yylex(); // declaration" of xxx warnings.
int yyparse();
%}
%union
{
llvm::Module* ModuleVal;
llvm::Function* FunctionVal;
llvm::BasicBlock* BasicBlockVal;
int64_t IntegerVal;
char* StringVal;
}
/* Typed Productions */
%type <ModuleVal> Module DefinitionList
%type <FunctionVal> Definition ForwardDef ColonDef MainDef
%type <FunctionVal> WordList
%type <BasicBlockVal> Word
/* Typed Tokens */
%token <IntegerVal> INTEGER
%token <StringVal> STRING IDENTIFIER
/* Terminal Tokens */
%token SEMI COLON FORWARD MAIN DUMP
%token TRUETOK FALSETOK LESS MORE LESS_EQUAL MORE_EQUAL NOT_EQUAL EQUAL
%token PLUS MINUS INCR DECR MULT DIV MODULUS NEGATE ABS MIN MAX STAR_SLASH
%token AND OR XOR LSHIFT RSHIFT
%token DROP DROP2 NIP NIP2 DUP DUP2 SWAP SWAP2 OVER OVER2 ROT ROT2
%token RROT RROT2 TUCK TUCK2 ROLL PICK SELECT
%token MALLOC FREE GET PUT
%token IF ELSE ENDIF WHILE END RECURSE RETURN EXIT
%token TAB SPACE CR IN_STR IN_NUM IN_CHAR OUT_STR OUT_NUM OUT_CHAR
/* Start Token */
%start Module
%%
/* A module is just a DefinitionList */
Module : { SCI->handle_module_start( ); }
DefinitionList { $$ = SCI->handle_module_end( $2 ); } ;
/* A Definitionlist is just a sequence of definitions */
DefinitionList : DefinitionList Definition { $$ = SCI->handle_definition_list_end( $1, $2 ); }
| /* empty */ { $$ = SCI->handle_definition_list_start(); } ;
/* A definition can be one of three flavors */
Definition : ForwardDef { $$ = $1; }
| ColonDef { $$ = $1; }
| MainDef { $$ = $1; } ;
/* Forward definitions just introduce a name */
ForwardDef : FORWARD IDENTIFIER SEMI { $$ = SCI->handle_forward( $2 ); } ;
/* The main definition has to generate additional code so we treat it specially */
MainDef : COLON MAIN WordList SEMI { $$ = SCI->handle_main_definition($3); } ;
/* Regular definitions have a name and a WordList */
ColonDef : COLON IDENTIFIER WordList SEMI { $$ = SCI->handle_definition( $2, $3 ); } ;
/* A WordList is just a sequence of words */
WordList : WordList Word { $$ = SCI->handle_word_list_end( $1, $2 ); }
| /* empty */ { $$ = SCI->handle_word_list_start(); } ;
/* A few "words" have a funky syntax */
/* FIXME: The body of compound words can currently only be function calls */
/* This is not acceptable, it should be a WordList, but that produces a Function */
/* Which is hard to merge into the function the compound statement is working on */
Word : IF IDENTIFIER ELSE IDENTIFIER ENDIF { $$ = SCI->handle_if( $2, $4 ); }
| IF IDENTIFIER ENDIF { $$ = SCI->handle_if( $2 ); }
| WHILE IDENTIFIER END { $$ = SCI->handle_while( $2 ); } ;
/* A few words are handled specially */
Word : IDENTIFIER { $$ = SCI->handle_identifier( $1 ); } ;
Word : STRING { $$ = SCI->handle_string( $1 ); } ;
Word : INTEGER { $$ = SCI->handle_integer( $1 ); } ;
/* Everything else is a terminal symbol and goes to handle_word */
Word : TRUETOK { $$ = SCI->handle_word( TRUETOK ); } ;
Word : FALSETOK { $$ = SCI->handle_word( FALSETOK ); } ;
Word : LESS { $$ = SCI->handle_word( LESS ); } ;
Word : MORE { $$ = SCI->handle_word( MORE ); } ;
Word : LESS_EQUAL { $$ = SCI->handle_word( LESS_EQUAL ); } ;
Word : MORE_EQUAL { $$ = SCI->handle_word( MORE_EQUAL ); } ;
Word : NOT_EQUAL { $$ = SCI->handle_word( NOT_EQUAL ); } ;
Word : EQUAL { $$ = SCI->handle_word( EQUAL ); } ;
Word : PLUS { $$ = SCI->handle_word( PLUS ); } ;
Word : MINUS { $$ = SCI->handle_word( MINUS ); } ;
Word : INCR { $$ = SCI->handle_word( INCR ); } ;
Word : DECR { $$ = SCI->handle_word( DECR ); } ;
Word : MULT { $$ = SCI->handle_word( MULT ); } ;
Word : DIV { $$ = SCI->handle_word( DIV ); } ;
Word : MODULUS { $$ = SCI->handle_word( MODULUS ); } ;
Word : NEGATE { $$ = SCI->handle_word( NEGATE ); } ;
Word : ABS { $$ = SCI->handle_word( ABS ); } ;
Word : MIN { $$ = SCI->handle_word( MIN ); } ;
Word : MAX { $$ = SCI->handle_word( MAX ); } ;
Word : STAR_SLASH { $$ = SCI->handle_word( STAR_SLASH ); } ;
Word : AND { $$ = SCI->handle_word( AND ); } ;
Word : OR { $$ = SCI->handle_word( OR ); } ;
Word : XOR { $$ = SCI->handle_word( XOR ); } ;
Word : LSHIFT { $$ = SCI->handle_word( LSHIFT ); } ;
Word : RSHIFT { $$ = SCI->handle_word( RSHIFT ); } ;
Word : DROP { $$ = SCI->handle_word( DROP ); } ;
Word : DROP2 { $$ = SCI->handle_word( DROP2 ); } ;
Word : NIP { $$ = SCI->handle_word( NIP ); } ;
Word : NIP2 { $$ = SCI->handle_word( NIP2 ); } ;
Word : DUP { $$ = SCI->handle_word( DUP ); } ;
Word : DUP2 { $$ = SCI->handle_word( DUP2 ); } ;
Word : SWAP { $$ = SCI->handle_word( SWAP ); } ;
Word : SWAP2 { $$ = SCI->handle_word( SWAP2 ); } ;
Word : OVER { $$ = SCI->handle_word( OVER ); } ;
Word : OVER2 { $$ = SCI->handle_word( OVER2 ); } ;
Word : ROT { $$ = SCI->handle_word( ROT ); } ;
Word : ROT2 { $$ = SCI->handle_word( ROT2 ); } ;
Word : RROT { $$ = SCI->handle_word( RROT ); } ;
Word : RROT2 { $$ = SCI->handle_word( RROT2 ); } ;
Word : TUCK { $$ = SCI->handle_word( TUCK ); } ;
Word : TUCK2 { $$ = SCI->handle_word( TUCK2 ); } ;
Word : ROLL { $$ = SCI->handle_word( ROLL ); } ;
Word : PICK { $$ = SCI->handle_word( PICK ); } ;
Word : SELECT { $$ = SCI->handle_word( SELECT ); } ;
Word : MALLOC { $$ = SCI->handle_word( MALLOC ); } ;
Word : FREE { $$ = SCI->handle_word( FREE ); } ;
Word : GET { $$ = SCI->handle_word( GET ); } ;
Word : PUT { $$ = SCI->handle_word( PUT ); } ;
Word : RECURSE { $$ = SCI->handle_word( RECURSE ); } ;
Word : RETURN { $$ = SCI->handle_word( RETURN ); } ;
Word : EXIT { $$ = SCI->handle_word( EXIT ); } ;
Word : TAB { $$ = SCI->handle_word( TAB ); };
Word : SPACE { $$ = SCI->handle_word( SPACE ); } ;
Word : CR { $$ = SCI->handle_word( CR ); } ;
Word : IN_STR { $$ = SCI->handle_word( IN_STR ); } ;
Word : IN_NUM { $$ = SCI->handle_word( IN_NUM ); } ;
Word : IN_CHAR { $$ = SCI->handle_word( IN_CHAR ); } ;
Word : OUT_STR { $$ = SCI->handle_word( OUT_STR ); } ;
Word : OUT_NUM { $$ = SCI->handle_word( OUT_NUM ); } ;
Word : OUT_CHAR { $$ = SCI->handle_word( OUT_CHAR ); } ;
Word : DUMP { $$ = SCI->handle_word( DUMP ); } ;
%%
/* Handle messages a little more nicely than the default yyerror */
int yyerror(const char *ErrorMsg) {
std::string where
= std::string((SCI->filename() == "-") ? std::string("<stdin>") : SCI->filename())
+ ":" + utostr((unsigned) Stackerlineno ) + ": ";
std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
if (yychar == YYEMPTY)
errMsg += "end-of-file.";
else
errMsg += "token: '" + std::string(Stackertext, Stackerleng) + "'";
StackerCompiler::ThrowException(errMsg);
return 0;
}