Defining string java cup/jflex

lionelv@gmail.com
8 Feb 2007 08:19:08 -0500

          From comp.compilers

Related articles
Defining string java cup/jflex lionelv@gmail.com (2007-02-08)
| List of all articles for this month |
From: lionelv@gmail.com
Newsgroups: comp.compilers
Date: 8 Feb 2007 08:19:08 -0500
Organization: Compilers Central
Keywords: Java, question
Posted-Date: 08 Feb 2007 08:19:07 EST

Hi all,


I'm a newbie to cup/jflex and lexical analysis.


I've taken an example for Scanner.jflex and Parser.cup and I'm slowly
trying to add then modify it to suit my purpose. I'm stuck on defining
a string. I want to know how I can recognise "astring" as a string in
the input below with "comment" being a keyword:


comment "astring";


The actual input file I'm using is:


8+4;
comment "astring";


Here is my source:


Scanner:


package Example;


import java_cup.runtime.SymbolFactory;
%%
%cup
%class Scanner
%{
public Scanner(java.io.InputStream r, SymbolFactory sf){
this(r);
this.sf=sf;
}
            StringBuffer string = new StringBuffer();
private SymbolFactory sf;
%}
%eofval{
        return sf.newSymbol("EOF",sym.EOF);
%eofval}


%state STRING


%%
";" { return sf.newSymbol("Semicolon",sym.SEMI); }
"+" { return sf.newSymbol("Plus",sym.PLUS); }
"*" { return sf.newSymbol("Times",sym.TIMES); }
"(" { return sf.newSymbol("Left Bracket",sym.LPAREN); }
")" { return sf.newSymbol("Right Bracket",sym.RPAREN); }
[0-9]+ { return sf.newSymbol("Integral Number",sym.NUMBER, new
Integer(yytext())); }
[ \t\r\n\f] { /* ignore white space. */ }
. { System.err.println("Illegal character: "+yytext()); }


\" { string.setLength(0); yybegin(STRING); }


<YYINITIAL> "comment" { return sf.newSymbol("Comment", sym.COMMENT); }


<STRING> {
    \" { yybegin(YYINITIAL);
                                                                      return symbol(sym.STRING_LITERAL,
                                                                      string.toString()); }
    [^\n\r\"\\]+ { string.append( yytext() ); }
    \\t { string.append('\t'); }
    \\n { string.append('\n'); }


    \\r { string.append('\r'); }
    \\\" { string.append('\"'); }
    \\ { string.append('\\'); }
}




Parser:


package Example;


import java_cup.runtime.*;


parser code {:
public static void main(String args[]) throws Exception {
SymbolFactory sf = new ComplexSymbolFactory();
if (args.length==0) new Parser(new
Scanner(System.in,sf),sf).parse();
else new Parser(new Scanner(new
java.io.FileInputStream(args[0]),sf),sf).parse();
}
:}


terminal COMMENT;
terminal String STRING;
terminal SEMI, PLUS, TIMES, LPAREN, RPAREN;
terminal Integer NUMBER;


non terminal expr_list, expr_part, type_data;
non terminal Integer expr;


precedence left PLUS;
precedence left TIMES;


expr_list ::= expr_list expr_part | expr_part | expr_list type_data;
expr_part ::= expr:e {: System.out.println(" = "+e+";"); :} SEMI;
expr ::= NUMBER:n
{: RESULT=n; :}
                        | expr:l PLUS expr:r
{: RESULT=new Integer(l.intValue() + r.intValue()); :}
| expr:l TIMES expr:r
{: RESULT=new Integer(l.intValue() * r.intValue()); :}
| LPAREN expr:e RPAREN
{: RESULT=e; :}
;
type_data ::= COMMENT {: System.out.println("bar"); :} STRING:str {:
System.out.println("bar1"); :} SEMI;




Ultimately I want a string to be defined between braces: {astring}


Can someone give me some guidance? Suggested starting point?


thanks


Lionel.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.