[cig-commits] r6535 - cs/babel/trunk/spike/Spike/Compiler
leif at geodynamics.org
leif at geodynamics.org
Tue Apr 10 02:58:19 PDT 2007
Author: leif
Date: 2007-04-10 02:58:18 -0700 (Tue, 10 Apr 2007)
New Revision: 6535
Modified:
cs/babel/trunk/spike/Spike/Compiler/gram.y
cs/babel/trunk/spike/Spike/Compiler/main.c
cs/babel/trunk/spike/Spike/Compiler/parser.pyx
Log:
Simplified the grammar considerably. In general, I merged stuff that
looked the same (same rule, same action). E.g., I merged 'declarator'
and 'abstract_declarator'. Of course, this places a greater burden
upon the semantic analysis phase.
Allow variable declarations anywhere within a block, as in C++ and
C99. Note that nested scopes aren't really supported yet, as the
underlying Pyrex engine doesn't support them.
Modified: cs/babel/trunk/spike/Spike/Compiler/gram.y
===================================================================
--- cs/babel/trunk/spike/Spike/Compiler/gram.y 2007-04-10 01:36:50 UTC (rev 6534)
+++ cs/babel/trunk/spike/Spike/Compiler/gram.y 2007-04-10 09:58:18 UTC (rev 6535)
@@ -177,10 +177,8 @@
;
declaration_specifiers
- : storage_class_specifier { CHECK($$ = declaration_specifiers($1, Py_None)); }
- | storage_class_specifier declaration_specifiers { CHECK($$ = declaration_specifiers($1, $2)); }
- | type_specifier { CHECK($$ = declaration_specifiers($1, Py_None)); }
- | type_specifier declaration_specifiers { CHECK($$ = declaration_specifiers($1, $2)); }
+ : declspec { CHECK($$ = declaration_specifiers(Py_None, $1)); }
+ | declaration_specifiers declspec { CHECK($$ = declaration_specifiers($1, $2)); }
;
init_declarator_list
@@ -193,16 +191,13 @@
| declarator '=' initializer
;
-storage_class_specifier
+declspec
: TYPEDEF
| EXTERN
| STATIC
| AUTO
| REGISTER
- ;
-
-type_specifier
- : CHAR
+ | CHAR
| SHORT
| INT
| LONG
@@ -238,7 +233,7 @@
;
struct_declaration
- : type_specifier_list struct_declarator_list ';'
+ : declaration_specifiers struct_declarator_list ';'
;
struct_declarator_list
@@ -269,31 +264,31 @@
;
declarator
- : declarator2
+ : pointer { CHECK($$ = pointer_declarator($1, empty_declarator())); }
+ | declarator2
| pointer declarator2 { CHECK($$ = pointer_declarator($1, $2)); }
;
declarator2
- : identifier { CHECK($$ = name_declarator($1)); }
- | '(' declarator ')' { $$ = $2; }
- | declarator2 '[' ']' { CHECK($$ = array_declarator($1, Py_None)); }
+ : identifier { CHECK($$ = name_declarator($1)); }
+ | '(' declarator ')' { $$ = $2; }
+ | '[' ']' { CHECK($$ = array_declarator(empty_declarator(), Py_None)); }
+ | '[' constant_expr ']' { CHECK($$ = array_declarator(empty_declarator(), $2)); }
+ | declarator2 '[' ']' { CHECK($$ = array_declarator($1, Py_None)); }
| declarator2 '[' constant_expr ']' { CHECK($$ = array_declarator($1, $3)); }
- | declarator2 '(' ')' { CHECK($$ = function_declarator($1, empty_list())); }
- | declarator2 '(' parameter_list ')' { CHECK($$ = function_declarator($1, $3)); }
+ | '(' ')' { CHECK($$ = function_declarator(empty_declarator(), empty_list())); }
+ | '(' parameter_list ')' { CHECK($$ = function_declarator(empty_declarator(), $2)); }
+ | declarator2 '(' ')' { CHECK($$ = function_declarator($1, empty_list())); }
+ | declarator2 '(' parameter_list ')' { CHECK($$ = function_declarator($1, $3)); }
;
pointer
- : '*' { CHECK($$ = pointer($1, Py_None, Py_None)); }
- | '*' type_specifier_list { CHECK($$ = pointer($1, $2, Py_None)); }
- | '*' pointer { CHECK($$ = pointer($1, Py_None, $2)); }
- | '*' type_specifier_list pointer { CHECK($$ = pointer($1, $2, $3)); }
+ : '*' { CHECK($$ = pointer($1, Py_None, Py_None)); }
+ | '*' declaration_specifiers { CHECK($$ = pointer($1, $2, Py_None)); }
+ | '*' pointer { CHECK($$ = pointer($1, Py_None, $2)); }
+ | '*' declaration_specifiers pointer { CHECK($$ = pointer($1, $2, $3)); }
;
-type_specifier_list
- : type_specifier { CHECK($$ = type_specifier_list(Py_None, $1)); }
- | type_specifier_list type_specifier { CHECK($$ = type_specifier_list($1, $2)); }
- ;
-
parameter_list
: parameter_declaration { CHECK($$ = parameter_list(Py_None, $1)); }
| parameter_list ',' parameter_declaration { CHECK($$ = parameter_list($1, $3)); }
@@ -301,35 +296,16 @@
;
parameter_declaration
- : type_specifier_list declarator { CHECK($$ = pair($1, $2)); }
- | type_specifier_list abstract_declarator { CHECK($$ = pair($1, $2)); }
- | type_specifier_list { CHECK($$ = pair($1, empty_declarator())); }
- | declarator { CHECK($$ = pair(Py_None, $1)); }
+ : declaration_specifiers declarator { CHECK($$ = pair($1, $2)); }
+ | declaration_specifiers { CHECK($$ = pair($1, empty_declarator())); }
+ | declarator { CHECK($$ = pair(Py_None, $1)); }
;
type_name
- : type_specifier_list { CHECK($$ = pair($1, empty_declarator())); }
- | type_specifier_list abstract_declarator { CHECK($$ = pair($1, $2)); }
+ : declaration_specifiers { CHECK($$ = pair($1, empty_declarator())); }
+ | declaration_specifiers declarator { CHECK($$ = pair($1, $2)); }
;
-abstract_declarator
- : pointer { CHECK($$ = pointer_declarator($1, empty_declarator())); }
- | abstract_declarator2
- | pointer abstract_declarator2 { CHECK($$ = pointer_declarator($1, $2)); }
- ;
-
-abstract_declarator2
- : '(' abstract_declarator ')' { CHECK($$ = $2); }
- | '[' ']' { CHECK($$ = array_declarator(empty_declarator(), Py_None)); }
- | '[' constant_expr ']' { CHECK($$ = array_declarator(empty_declarator(), $2)); }
- | abstract_declarator2 '[' ']' { CHECK($$ = array_declarator($1, Py_None)); }
- | abstract_declarator2 '[' constant_expr ']' { CHECK($$ = array_declarator($1, $3)); }
- | '(' ')' { CHECK($$ = function_declarator(empty_declarator(), empty_list())); }
- | '(' parameter_list ')' { CHECK($$ = function_declarator(empty_declarator(), $2)); }
- | abstract_declarator2 '(' ')' { CHECK($$ = function_declarator($1, empty_list())); }
- | abstract_declarator2 '(' parameter_list ')' { CHECK($$ = function_declarator($1, $3)); }
- ;
-
initializer
: assignment_expr
| '{' initializer_list '}'
@@ -387,6 +363,7 @@
: labeled_statement
| compound_statement
| expression_statement
+ | declaration
| selection_statement
| iteration_statement
| jump_statement
@@ -399,20 +376,13 @@
;
compound_statement
- : '{' '}' { CHECK($$ = compound_statement($1, Py_None, Py_None)); }
- | '{' statement_list '}' { CHECK($$ = compound_statement($1, Py_None, $2)); }
- | '{' declaration_list '}' { CHECK($$ = compound_statement($1, $2, Py_None)); }
- | '{' declaration_list statement_list '}' { CHECK($$ = compound_statement($1, $2, $3)); }
+ : '{' '}' { CHECK($$ = statement_list($1, Py_None, Py_None)); }
+ | '{' statement_list '}' { CHECK($$ = $2); }
;
-declaration_list
- : declaration { CHECK($$ = declaration_list(Py_None, $1)); }
- | declaration_list declaration { CHECK($$ = declaration_list($1, $2)); }
- ;
-
statement_list
- : statement { CHECK($$ = statement_list(Py_None, $1)); }
- | statement_list statement { CHECK($$ = statement_list($1, $2)); }
+ : statement { CHECK($$ = statement_list(Py_None, Py_None, $1)); }
+ | statement_list statement { CHECK($$ = statement_list(Py_None, $1, $2)); }
;
expression_statement
@@ -428,16 +398,9 @@
;
iteration_statement
- : WHILE '(' expr ')' statement { CHECK($$ = while_statement($1, $3, $5)); }
+ : WHILE '(' expr ')' statement { CHECK($$ = while_statement($1, $3, $5)); }
| DO statement WHILE '(' expr ')' ';'
- | FOR '(' ';' ';' ')' statement
- | FOR '(' ';' ';' expr ')' statement
- | FOR '(' ';' expr ';' ')' statement
- | FOR '(' ';' expr ';' expr ')' statement
- | FOR '(' expr ';' ';' ')' statement
- | FOR '(' expr ';' ';' expr ')' statement
- | FOR '(' expr ';' expr ';' ')' statement
- | FOR '(' expr ';' expr ';' expr ')' statement
+ | FOR '(' statement_list expr ')' statement { CHECK($$ = pass_statement($1)); }
;
jump_statement
@@ -465,9 +428,14 @@
function_body
: compound_statement
- | declaration_list compound_statement
+ | function_prologue compound_statement { CHECK($$ = $2); /*TODO*/ }
;
+function_prologue
+ : declaration
+ | function_prologue declaration
+ ;
+
identifier
: IDENTIFIER { CHECK($$ = $1); }
;
Modified: cs/babel/trunk/spike/Spike/Compiler/main.c
===================================================================
--- cs/babel/trunk/spike/Spike/Compiler/main.c 2007-04-10 01:36:50 UTC (rev 6534)
+++ cs/babel/trunk/spike/Spike/Compiler/main.c 2007-04-10 09:58:18 UTC (rev 6535)
@@ -12,8 +12,15 @@
int yyparse();
+#ifdef YYDEBUG
+extern int yydebug;
+#endif
+
int main(int argc, char **argv)
{
+#ifdef YYDEBUG
+ yydebug = 1;
+#endif
/* add our extension modules */
if (PyImport_ExtendInittab(inittab) == -1) {
fprintf(stderr, "%s: PyImport_ExtendInittab failed! Exiting...\n", argv[0]);
Modified: cs/babel/trunk/spike/Spike/Compiler/parser.pyx
===================================================================
--- cs/babel/trunk/spike/Spike/Compiler/parser.pyx 2007-04-10 01:36:50 UTC (rev 6534)
+++ cs/babel/trunk/spike/Spike/Compiler/parser.pyx 2007-04-10 09:58:18 UTC (rev 6535)
@@ -213,10 +213,10 @@
cdef public object declaration(object declaration_specifiers, object init_declarator_list):
return declaration_specifiers.declaration(init_declarator_list)
-cdef public object declaration_specifiers(object specifier, object declaration_specifiers):
+cdef public object declaration_specifiers(object declaration_specifiers, object declspec):
if declaration_specifiers is None:
declaration_specifiers = DeclSpecs()
- declaration_specifiers.add(specifier)
+ declaration_specifiers.add(declspec)
return declaration_specifiers
cdef public object init_declarator_list(object init_declarator_list, object init_declarator):
@@ -265,12 +265,6 @@
modifiers = type_specifier_list.analyse_type_modifiers()
return CPtrDeclaratorNode(pos, base = base)
-cdef public object type_specifier_list(object type_specifier_list, object type_specifier):
- if type_specifier_list is None:
- type_specifier_list = DeclSpecs()
- type_specifier_list.add(type_specifier)
- return type_specifier_list
-
cdef public object parameter_list(object parameter_list, object parameter_declaration):
if parameter_list is None:
parameter_list = []
@@ -302,26 +296,15 @@
WhileStatNode, \
ReturnStatNode
-cdef public object compound_statement(object lcurly, object declaration_list, object statement_list):
- _, pos = lcurly
- stats = []
- if declaration_list is not None:
- stats.extend(declaration_list.stats)
- if statement_list is not None:
- stats.extend(statement_list.stats)
- ret = StatListNode(pos, stats = stats)
- return ret
-
-cdef public object declaration_list(object declaration_list, object declaration):
- if declaration_list is None:
- declaration_list = StatListNode(declaration.pos, stats = [])
- declaration_list.stats.extend(declaration.stats)
- return declaration_list
-
-cdef public object statement_list(object statement_list, object statement):
+cdef public object statement_list(object lcurly, object statement_list, object statement):
+ if lcurly is None:
+ pos = statement.pos
+ else:
+ pos = lcurly[1]
if statement_list is None:
- statement_list = StatListNode(statement.pos, stats = [])
- statement_list.stats.append(statement)
+ statement_list = StatListNode(pos, stats = [])
+ if statement is not None:
+ statement_list.stats.append(statement)
return statement_list
cdef public object pass_statement(object semicolon):
More information about the cig-commits
mailing list