modified example programs, added strings to lexer

git-svn-id: svn://anubis/jtlc/trunk@6 f5bc74b8-7b62-4e90-9214-7121d538519f
This commit is contained in:
josh 2010-01-12 20:23:08 +00:00
parent e83a2f1f08
commit bb982b62b8
5 changed files with 41 additions and 7 deletions

View File

@ -5,9 +5,14 @@
%{ %{
#include "parser.tab.hh" #include "parser.tab.hh"
#include <string>
using namespace std;
static string build_string;
%} %}
%x str
%% %%
/* operators */ /* operators */
@ -66,6 +71,8 @@ int return INT;
uint return UINT; uint return UINT;
long return LONG; long return LONG;
ulong return ULONG; ulong return ULONG;
float return FLOAT;
double return DOUBLE;
/* keywords */ /* keywords */
import return IMPORT; import return IMPORT;
@ -76,6 +83,31 @@ struct return STRUCT;
/* identifiers */ /* identifiers */
[a-zA-Z_][a-zA-Z_0-9]* { [a-zA-Z_][a-zA-Z_0-9]* {
return IDENTIFIER; return IDENTIFIER;
}
/* strings */
\" build_string = ""; BEGIN(str);
<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 */ /* whitespace */

View File

@ -59,6 +59,7 @@ int yywrap()
/* literals */ /* literals */
%token INT_LITERAL; %token INT_LITERAL;
%token REAL_LITERAL; %token REAL_LITERAL;
%token STRING_LITERAL;
/* primitive types */ /* primitive types */
%token BYTE; %token BYTE;
@ -71,6 +72,8 @@ int yywrap()
%token UINT; %token UINT;
%token LONG; %token LONG;
%token ULONG; %token ULONG;
%token FLOAT;
%token DOUBLE;
/* keywords */ /* keywords */
%token IMPORT; %token IMPORT;

View File

@ -1,7 +1,7 @@
import std.io.*; import std.io.*;
int main(args : String[]) public main(args : String[]) : int
{ {
foreach (arg <- args) foreach (arg <- args)
println("arg: ", arg); println("arg: ", arg);

View File

@ -5,7 +5,7 @@ import std.io.*;
typedef function double (double) interpolator_t; typedef function double (double) interpolator_t;
int main(args : String[]) public main(args : String[]) : int
{ {
interpolator_t i1 = getInterpolator(1, 5, 4); interpolator_t i1 = getInterpolator(1, 5, 4);
interpolator_t i2 = getInterpolator(10, 2, 5); interpolator_t i2 = getInterpolator(10, 2, 5);
@ -26,7 +26,7 @@ int main(args : String[])
* Create and return a linear interpolator functor which interpolates * Create and return a linear interpolator functor which interpolates
* between (0.0, x1) and (sep, x2), returning the output y-value * 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); assert (sep > 0.0);

View File

@ -3,7 +3,7 @@ module Regex;
import std.io.*; import std.io.*;
int main(args : String[]) public main(args : String[]) : int
{ {
foreach (arg <- args) foreach (arg <- args)
{ {
@ -20,15 +20,14 @@ int main(args : String[])
{ {
println(arg, " is a number"); 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"); println(arg, " is a hexadecimal number");
} }
else else
{ {
/* auto declaration of matches to be of type String[] */ /* auto declaration of matches to be of type String[] */
matches ::= arg =~ "/(.)(.)/"; if (matches ::= arg =~ "/(.)(.)/")
if (matches)
{ {
println("first character: ", matches[0], println("first character: ", matches[0],
"second character: ", matches[1]); "second character: ", matches[1]);