Skip to content

Commit

Permalink
Improving the code generator and updating targets
Browse files Browse the repository at this point in the history
  • Loading branch information
phorward committed Dec 13, 2017
1 parent 01e690d commit fd76474
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 71 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ This file is used to document any relevant changes done to UniCC.

Released on: Not released yet.

- Code generator does not emit action code when the action is empty.
- New command-line parameter "-l" or "--language" to specify a target language
via command-line. This is useful when a grammar only contains AST definitions,
which allows to render in any target.
which allows to render and run it in any target.
- Abstract syntax tree generation support:
- Drafting an abstract syntax tree definition syntax for the grammar language,
by using the "=" operator.
- See examples/expr.ast.par for an example working with C and Python
by using the operators "=" and ":=". See examples/expr.ast.par for an
example working under both C and Python without any modifications.
- C and Python parser targets extended to construct abstract syntax tree
data structures dynamically.
- These are first attempts for an upcoming UniCC v2, where the targets shall
- These are first changes for an upcoming UniCC v2, where the targets shall
be re-used with only few improvements.
- Fixed several bugs in the Python target on insensitive mode.
- Additionally allow ":" in case of "->" in grammar definitions.
Expand All @@ -24,7 +25,7 @@ Released on: Not released yet.

## v1.2

Released on: December 7, 2017
Released on: November 7, 2017

- Started an (yet incomplete) new standard template to provide parser generation
support for the Python programming language (targets/python.tlt).
Expand Down
22 changes: 13 additions & 9 deletions build.c
Original file line number Diff line number Diff line change
Expand Up @@ -1238,10 +1238,6 @@ void build_code( PARSER* parser )
{
p = (PROD*)plist_access( e );

actions = pstrcatstr( actions, pstrrender( gen->action_start,
GEN_WILD_PREFIX "production-number", int_to_str( p->id ), TRUE,
(char*)NULL ), TRUE );

/* Select the semantic code to be processed! */
act = (char*)NULL;

Expand All @@ -1268,8 +1264,14 @@ void build_code( PARSER* parser )
act = (char*)NULL;
}

if( act )
if( act && *act )
{
/* Generate action start */
actions = pstrcatstr( actions, pstrrender( gen->action_start,
GEN_WILD_PREFIX "production-number", int_to_str( p->id ), TRUE,
(char*)NULL ), TRUE );

/* Generate code localization */
if( gen->code_localization && p->code_at > 0 )
{
actions = pstrcatstr( actions,
Expand All @@ -1280,13 +1282,15 @@ void build_code( PARSER* parser )
TRUE );
}

/* Generate the action code */
act = build_action( parser, gen, p, act, is_default_code );
actions = pstrcatstr( actions, act, TRUE );
}

actions = pstrcatstr( actions, pstrrender( gen->action_end,
GEN_WILD_PREFIX "production-number", int_to_str( p->id ), TRUE,
(char*)NULL ), TRUE );
/* Generate the action end */
actions = pstrcatstr( actions, pstrrender( gen->action_end,
GEN_WILD_PREFIX "production-number", int_to_str( p->id ), TRUE,
(char*)NULL ), TRUE );
}

/* Generate production information table */
productions = pstrcatstr( productions, pstrrender(
Expand Down
47 changes: 1 addition & 46 deletions parse.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Parser module generated from parse.par.
Parser module generated by unicc from parse.par.
DO NOT EDIT THIS FILE MANUALLY, IT WILL GO AWAY!
*/

Expand Down Expand Up @@ -3863,21 +3863,6 @@ int _parse( _pcb* pcb )
reset_strbuf();
strbuf_append( ( ( pcb->tos - 0 )->value.value_5 ) );

;
} break;
case 159:
{

;
} break;
case 160:
{

;
} break;
case 161:
{

;
} break;
case 162:
Expand Down Expand Up @@ -3905,11 +3890,6 @@ int _parse( _pcb* pcb )
{
pcb->ret.value_5 = 0;
;
} break;
case 167:
{

;
} break;
case 168:
{
Expand Down Expand Up @@ -3956,31 +3936,6 @@ int _parse( _pcb* pcb )
{
pcb->ret.value_5 = ( ( pcb->tos - 1 )->value.value_5 );
;
} break;
case 176:
{

;
} break;
case 177:
{

;
} break;
case 178:
{

;
} break;
case 179:
{

;
} break;
case 180:
{

;
} break;
case 181:
{
Expand Down
2 changes: 1 addition & 1 deletion parse.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Parser header generated from parse.par.
Parser header generated by unicc from parse.par.
DO NOT EDIT THIS FILE MANUALLY, IT WILL GO AWAY!
*/

Expand Down
12 changes: 10 additions & 2 deletions targets/C.source/STDTPL.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ the UniCC Standard C Parser Template.
<!-- Escape double quotation marks -->
<escape-sequence for="&#x22;" do="\&#x22;" />

<!--
****************************************************************************
The unset value
****************************************************************************
This defines the value that stands for Nothing, Empty, Unset, 0.
-->
<null-value>NULL</null-value>

<!--
****************************************************************************
Value-stack templates
Expand Down Expand Up @@ -746,7 +754,7 @@ the UniCC Standard C Parser Template.
the filename-attribute of the file-tag, to generate adequate filenames.
-->
<file filename="@@basename.c">/*
Parser module generated from @@filename.
Parser module generated by unicc from @@filename.
DO NOT EDIT THIS FILE MANUALLY, IT WILL GO AWAY!
*/

Expand Down Expand Up @@ -796,7 +804,7 @@ the UniCC Standard C Parser Template.

</file>
<file filename="@@basename.h">/*
Parser header generated from @@filename.
Parser header generated by unicc from @@filename.
DO NOT EDIT THIS FILE MANUALLY, IT WILL GO AWAY!
*/

Expand Down
12 changes: 10 additions & 2 deletions targets/c.tlt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ the UniCC Standard C Parser Template.
<!-- Escape double quotation marks -->
<escape-sequence for="&#x22;" do="\&#x22;" />

<!--
****************************************************************************
The unset value
****************************************************************************
This defines the value that stands for Nothing, Empty, Unset, 0.
-->
<null-value>NULL</null-value>

<!--
****************************************************************************
Value-stack templates
Expand Down Expand Up @@ -746,7 +754,7 @@ the UniCC Standard C Parser Template.
the filename-attribute of the file-tag, to generate adequate filenames.
-->
<file filename="@@basename.c">/*
Parser module generated from @@filename.
Parser module generated by unicc from @@filename.
DO NOT EDIT THIS FILE MANUALLY, IT WILL GO AWAY!
*/

Expand Down Expand Up @@ -2109,7 +2117,7 @@ int main( int argc, char** argv )

</file>
<file filename="@@basename.h">/*
Parser header generated from @@filename.
Parser header generated by unicc from @@filename.
DO NOT EDIT THIS FILE MANUALLY, IT WILL GO AWAY!
*/

Expand Down
22 changes: 16 additions & 6 deletions targets/python.tlt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<generator name="Python" long-name="UniCC Standard Python Template" version="0.3">
<escape-sequence for="\" do="\\" />
<escape-sequence for="&#x22;" do="\&#x22;" />
<null-value>None</null-value>
<code_localization />

<vstack_def_type>any</vstack_def_type>
Expand All @@ -21,7 +22,7 @@

<action_set_lhs>pcb.lhs = @@sym</action_set_lhs>

<scan_action_start>&#x09;def _scan_action_@@symbol-number(self, shift = True):&#x0A;&#x09;&#x09;</scan_action_start>
<scan_action_start>&#x09;def _scan_action_@@symbol-number(self, pcb, shift = True):&#x0A;&#x09;&#x09;</scan_action_start>
<scan_action_end>&#x0A;&#x09;&#x09;return&#x0A;&#x0A;</scan_action_end>
<scan_action_begin_offset>pcb.buf</scan_action_begin_offset>
<scan_action_end_offset>pcb.length</scan_action_end_offset>
Expand Down Expand Up @@ -94,6 +95,8 @@
<file filename="@@basename.py">#!/usr/bin/python
#-*- coding: utf-8 -*-

# Parser module generated by unicc from @@filename.
# DO NOT EDIT THIS FILE MANUALLY, IT WILL GO AWAY!
@@prologue

class @@prefixNode(object):
Expand Down Expand Up @@ -200,6 +203,12 @@ class @@prefixParser(object):
@@dfa-accept
)

# Parsing actions
@@scan_actions
@@actions

# Parsing algorithm

def _get_act(self, pcb):
# Get action table entry

Expand Down Expand Up @@ -351,9 +360,6 @@ class @@prefixParser(object):

return pcb.sym &gt; -1

@@scan_actions
@@actions

def parse(self, s = None):
if s is None:
try:
Expand Down Expand Up @@ -393,7 +399,9 @@ class @@prefixParser(object):
pcb.stack.append(pcb.tos)

# Execute scanner actions, if existing.
#fn = getattr(self, "scan_action_%d" % pcb.sym)()
scan_fn = getattr(self, "_scan_action_%d" % pcb.sym, None)
if scan_fn:
scan_fn(pcb)

pcb.tos.state = -1 if pcb.act &amp; self._REDUCE else pcb.idx
pcb.tos.symbol = self._symbols[pcb.sym]
Expand All @@ -420,7 +428,9 @@ class @@prefixParser(object):

# Call reduce function
#print("CALL", "_reduce_action_%d" % pcb.idx)
getattr(self, "_reduce_action_%d" % pcb.idx)(pcb)
reduce_fn = getattr(self, "_reduce_action_%d" % pcb.idx, None)
if reduce_fn:
reduce_fn(pcb)

# Drop right-hand side
cnodes = None
Expand Down

0 comments on commit fd76474

Please sign in to comment.