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 <string>
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);
<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 */

View File

@ -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;

View File

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

View File

@ -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);

View File

@ -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]);