From bb982b62b89e484be8e570c704d195feb333d88d Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 12 Jan 2010 20:23:08 +0000 Subject: [PATCH] modified example programs, added strings to lexer git-svn-id: svn://anubis/jtlc/trunk@6 f5bc74b8-7b62-4e90-9214-7121d538519f --- parser/parser.lex | 32 ++++++++++++++++++++++++++++++++ parser/parser.yy | 3 +++ tests/HelloWorld.jtl | 2 +- tests/Interpolator.jtl | 4 ++-- tests/Regex.jtl | 7 +++---- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/parser/parser.lex b/parser/parser.lex index 42b48b2..d39b5a8 100644 --- a/parser/parser.lex +++ b/parser/parser.lex @@ -5,9 +5,14 @@ %{ #include "parser.tab.hh" +#include +using namespace std; +static string build_string; %} +%x str + %% /* operators */ @@ -66,6 +71,8 @@ int return INT; uint return UINT; long return LONG; ulong return ULONG; +float return FLOAT; +double return DOUBLE; /* keywords */ import return IMPORT; @@ -76,6 +83,31 @@ struct return STRUCT; /* identifiers */ [a-zA-Z_][a-zA-Z_0-9]* { return IDENTIFIER; +} + + /* strings */ +\" build_string = ""; BEGIN(str); +{ + +\" { + /* end of the string literal */ + BEGIN(INITIAL); + return STRING_LITERAL; +} +\\x[0-9A-Fa-f]{2} { + /* hexadecimal escape code */ + unsigned int val; + (void) sscanf(yytext + 2, "%x", &val); + build_string += (char) val; +} +\\n build_string += '\n'; +\\t build_string += '\t'; +\\r build_string += '\r'; +\\b build_string += '\b'; +\\f build_string += '\f'; +\\(.|\n) build_string += yytext[1]; +[^\\\"]+ build_string += yytext; + } /* whitespace */ diff --git a/parser/parser.yy b/parser/parser.yy index 6d73a5b..d7a3968 100644 --- a/parser/parser.yy +++ b/parser/parser.yy @@ -59,6 +59,7 @@ int yywrap() /* literals */ %token INT_LITERAL; %token REAL_LITERAL; +%token STRING_LITERAL; /* primitive types */ %token BYTE; @@ -71,6 +72,8 @@ int yywrap() %token UINT; %token LONG; %token ULONG; +%token FLOAT; +%token DOUBLE; /* keywords */ %token IMPORT; diff --git a/tests/HelloWorld.jtl b/tests/HelloWorld.jtl index e679c7f..29bc7bf 100644 --- a/tests/HelloWorld.jtl +++ b/tests/HelloWorld.jtl @@ -1,7 +1,7 @@ import std.io.*; -int main(args : String[]) +public main(args : String[]) : int { foreach (arg <- args) println("arg: ", arg); diff --git a/tests/Interpolator.jtl b/tests/Interpolator.jtl index 902ff2b..37e09f0 100644 --- a/tests/Interpolator.jtl +++ b/tests/Interpolator.jtl @@ -5,7 +5,7 @@ import std.io.*; typedef function double (double) interpolator_t; -int main(args : String[]) +public main(args : String[]) : int { interpolator_t i1 = getInterpolator(1, 5, 4); interpolator_t i2 = getInterpolator(10, 2, 5); @@ -26,7 +26,7 @@ int main(args : String[]) * Create and return a linear interpolator functor which interpolates * between (0.0, x1) and (sep, x2), returning the output y-value */ -interpolator_t getInterpolator(x1 : double, x2 : double, sep : double) +private getInterpolator(x1 : double, x2 : double, sep : double) : interpolator_t { assert (sep > 0.0); diff --git a/tests/Regex.jtl b/tests/Regex.jtl index 2158a14..190b09a 100644 --- a/tests/Regex.jtl +++ b/tests/Regex.jtl @@ -3,7 +3,7 @@ module Regex; import std.io.*; -int main(args : String[]) +public main(args : String[]) : int { foreach (arg <- args) { @@ -20,15 +20,14 @@ int main(args : String[]) { println(arg, " is a number"); } - else if (arg =~ "/^0x\d+$/i") + else if (arg =~ "/^0x[0-9a-fA-F]+$/i") { println(arg, " is a hexadecimal number"); } else { /* auto declaration of matches to be of type String[] */ - matches ::= arg =~ "/(.)(.)/"; - if (matches) + if (matches ::= arg =~ "/(.)(.)/") { println("first character: ", matches[0], "second character: ", matches[1]);