Commit 1b0e59db authored by petter's avatar petter

Allow specifying class names in the specification file


git-svn-id: https://www2.in.tum.de/repos/cup/develop@57 f8377d4f-c7f8-0310-b030-a31f13008fac
parent 1808e017
......@@ -230,7 +230,7 @@ terminal
PACKAGE, IMPORT, CODE, ACTION, PARSER, TERMINAL, NON, INIT, SCAN, WITH,
START, SEMI, COMMA, STAR, DOT, COLON, COLON_COLON_EQUALS, BAR, PRECEDENCE,
LEFT, RIGHT, NONASSOC, PERCENT_PREC, LBRACK, RBRACK, NONTERMINAL, GT, LT,
QUESTION, SUPER, EXTENDS;
QUESTION, SUPER, EXTENDS, CLASS;
terminal String ID, CODE_STRING;
......@@ -242,7 +242,7 @@ non terminal
type_id, term_name_list, non_term_name_list, production, prod_part_list,
prod_part, new_term_id, new_non_term_id, rhs_list, rhs, empty,
precedence_list, preced, terminal_list, precedence_l, declares_term,
declares_non_term;
declares_non_term, class_name;
non terminal String nt_id, symbol_id, label_id, opt_label, terminal_id,
term_id, robust_id, typearglist, typearguement, wildcard;
......@@ -263,6 +263,7 @@ spec ::=
:}
package_spec
import_list
class_name
code_parts
symbol_list
precedence_list
......@@ -322,6 +323,15 @@ import_spec ::=
SEMI
;
class_name ::=
empty |
CLASS ID:id SEMI
{:
emit.parser_class_name = id;
emit.symbol_const_class_name = id + "Sym";
:}
;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
// allow any order; all parts are optional. [CSA, 23-Jul-1999]
......
......@@ -76,6 +76,7 @@ ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*
"{:" { sb = new StringBuffer(); csline=yyline+1; cscolumn=yycolumn+1; yybegin(CODESEG); }
"package" { return symbol("PACKAGE",PACKAGE); }
"import" { return symbol("IMPORT",IMPORT); }
"class" { return symbol("CLASS",CLASS); }
"code" { return symbol("CODE",CODE); }
"action" { return symbol("ACTION",ACTION); }
"parser" { return symbol("PARSER",PARSER); }
......
......@@ -366,6 +366,7 @@ a total of eight specific parts (however, most of these are optional).
A specification consists of:
</p><ul>
<li> <a href="#package_spec">package and import specifications</a>,
</li><li> <a href="#parser_class_name">parser class name</a>,
</li><li> <a href="#code_part">user code components</a>,
</li><li> <a href="#symbol_list">symbol (terminal and non-terminal) lists</a>,
</li><li> <a href="#precedence">precedence declarations</a>, and
......@@ -407,10 +408,23 @@ Any import declarations that appear in the specification will also appear
in the source file for the <tt>parser</tt> class allowing various names from
that package to be used directly in user supplied action code.
After the package and imports, a specification can include a parser
class name, as follows:
<pre><tt> class <i>name</i>;</tt>
</pre>
If a class name is supplied, then that is the name that will be used
for the generated parser code. As well, the generated symbols class
will have the given name followed by "<tt>Sym</tt>".
<p>If no class name is supplied, then the parser class is called
"<tt>parser</tt>", and the symbols class is called "<tt>sym</tt>".
<h4><a name="code_part"></a>2.2 User Code Components</h4>
Following the optional <tt>package</tt> and <tt>import</tt> declarations
are a series of optional declarations that allow user code to be included
Following these declarations are another series of optional declarations
that allow user code to be included
as part of the generated parser (see <a href="#parser">Section 4</a> for a
full description of how the parser uses this code). As a part of the parser
file, a separate non-public class to contain all embedded user actions is
......@@ -1264,12 +1278,13 @@ Redwood City, CA,
<h3><a name="appendixa" />
Appendix A. Grammar for CUP Specification Files</a> (0.10j)</h3>
<hr><br>
<pre><tt>java_cup_spec ::= package_spec import_list code_parts
<pre><tt>java_cup_spec ::= package_spec import_list class_name code_parts
symbol_list precedence_list start_spec
production_list
package_spec ::= PACKAGE multipart_id SEMI | empty
import_list ::= import_list import_spec | empty
import_spec ::= IMPORT import_id SEMI
class_name ::= CLASS ID SEMI | empty
code_part ::= action_code_part | parser_code_part |
init_code | scan_code
code_parts ::= code_parts code_part | empty
......
......@@ -30,6 +30,7 @@ expr ::= expr:e1 PLUS expr:e2 {: RESULT = e1+e2; :}
| expr:e1 MINUS expr:e2 {: RESULT = e1-e2; :}
| expr:e1 TIMES expr:e2 {: RESULT = e1*e2; :}
| MINUS expr:e {: RESULT = -e; :}
%prec UMINUS
| LPAREN expr:e RPAREN {: RESULT = e; :}
| NUMBER:n {: RESULT = n; :}
;
......@@ -4,4 +4,4 @@ jflex minijava.jflex
java -jar ../../dist/java-cup-11b.jar -locations -interface -parser Parser -xmlactions minijava.cup
javac -cp ../../dist/java-cup-11b-runtime.jar:. *.java
java -cp ../../dist/java-cup-11b-runtime.jar:. Parser simple.minijava simple.xml
basex codegen.sq output.xml > simple.minijvm
\ No newline at end of file
basex codegen.sq output.xml > simple.minijvm
......@@ -8,6 +8,7 @@ import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
parser code {:
public Parser(Lexer lex, ComplexSymbolFactory sf) {
super(lex,sf);
......
int x, y;
y = 10;
x = read();
read();
while (1-y <= -x) {
y = x * (-y + 5);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment