diff options
Diffstat (limited to 'taccle/examples')
-rw-r--r-- | taccle/examples/Makefile | 17 | ||||
-rw-r--r-- | taccle/examples/if_then_else.tac | 18 | ||||
-rw-r--r-- | taccle/examples/infix_calc.tac | 35 | ||||
-rw-r--r-- | taccle/examples/interactive_calculator.tac | 42 | ||||
-rw-r--r-- | taccle/examples/lalr_reduce_reduce.tac | 18 | ||||
-rw-r--r-- | taccle/examples/reduce_reduce.tac | 20 | ||||
-rw-r--r-- | taccle/examples/reduce_reduce2.tac | 15 | ||||
-rw-r--r-- | taccle/examples/shift_reduce.tac | 17 | ||||
-rw-r--r-- | taccle/examples/shift_reduce2.tac | 20 | ||||
-rw-r--r-- | taccle/examples/simple_calculator.tac | 36 | ||||
-rw-r--r-- | taccle/examples/simple_expressions.tac | 30 | ||||
-rw-r--r-- | taccle/examples/simple_grammar.tac | 27 | ||||
-rw-r--r-- | taccle/examples/simple_scanner.fcl | 16 |
13 files changed, 311 insertions, 0 deletions
diff --git a/taccle/examples/Makefile b/taccle/examples/Makefile new file mode 100644 index 0000000..7235aea --- /dev/null +++ b/taccle/examples/Makefile @@ -0,0 +1,17 @@ +# $Id: Makefile,v 1.2 2004/09/29 16:23:28 tang Exp $ + +TCL=tclsh +FICKLE=~/fickle/fickle.tcl +TACCLE=../taccle.tcl +EXAMPLES=interactive_calculator.tcl simple_calculator.tcl infix_calc.tcl + +all: $(EXAMPLES) simple_scanner.tcl + +%.tcl: %.fcl + $(TCL) $(FICKLE) $< + +%.tcl: %.tac + $(TCL) $(TACCLE) -d -v -w $< + +clean: + -rm -f *tcl *output diff --git a/taccle/examples/if_then_else.tac b/taccle/examples/if_then_else.tac new file mode 100644 index 0000000..eee631a --- /dev/null +++ b/taccle/examples/if_then_else.tac @@ -0,0 +1,18 @@ +# $Id: if_then_else.tac,v 1.1 2004/08/18 23:53:42 tang Exp $ + +# The classical if/then/else ambiguity. taccle resolves this by +# giving higher precedence to shifting else. +# +# References: +# Dragon book, page 250 +# lex & yacc, pages 233-235 + +%token IF ELSE TERMINAL + +%% + +stmt: IF '(' cond ')' stmt + | IF '(' cond ')' stmt ELSE stmt + | TERMINAL; + +cond: TERMINAL; diff --git a/taccle/examples/infix_calc.tac b/taccle/examples/infix_calc.tac new file mode 100644 index 0000000..95ef2ee --- /dev/null +++ b/taccle/examples/infix_calc.tac @@ -0,0 +1,35 @@ +# $Id: infix_calc.tac,v 1.1 2004/09/29 16:23:28 tang Exp $ + +# This example demonstrates how taccle handles operator precedence. +# The code is shamelessly borrowed from the GNU Bison info manual. + +# taccle Declarations +%token ID NEWLINE +%left '-' '+' +%left '*' '/' +%left NEG # negation--unary minus +%right '^' # exponentiation + +# Grammar follows +%% +input: # empty string + | input line +; + +line: NEWLINE + | exp NEWLINE { puts [format "\t%.10g" $1] } +; + +exp: ID { set _ $1 } + | exp '+' exp { set _ [expr {$1 + $3}] } + | exp '-' exp { set _ [expr {$1 - $3}] } + | exp '*' exp { set _ [expr {$1 * $3}] } + | exp '/' exp { set _ [expr {$1 / $3}] } + | '-' exp %prec NEG { set _ [expr {-1.0 * $2}] } + | exp '^' exp { set _ [expr {pow($1, $3)}] } + | '(' exp ')' { set _ $2 } +; +%% + +source simple_scanner.tcl +yyparse diff --git a/taccle/examples/interactive_calculator.tac b/taccle/examples/interactive_calculator.tac new file mode 100644 index 0000000..ac8a3ba --- /dev/null +++ b/taccle/examples/interactive_calculator.tac @@ -0,0 +1,42 @@ +# $Id: interactive_calculator.tac,v 1.2 2004/09/08 21:38:44 tang Exp $ + +# This example expands the simple calculator to be interactive from +# the command line. Note the use of an empty rule (i.e., epsilon +# transition). Also featured are the error token and error recovery. + +%{ +#!/usr/bin/tclsh + +%} + +%token ID NEWLINE + +%% + +start: line NEWLINE start + | line + ; + +line: E { puts " = $1" } + | error { puts " -- error" } + | # empty + ; + +E: E '+' T { set _ [expr {$1 + $3}] } + | E '-' T { set _ [expr {$1 - $3}] } + | T + ; + +T: T '*' F { set _ [expr {$1 * $3}] } + | T '/' F { set _ [expr {$1 / $3}] } + | F + ; + +F: '(' E ')' { set _ $2 } + | ID { set _ $::yylval } + ; + +%% + +source simple_scanner.tcl +yyparse diff --git a/taccle/examples/lalr_reduce_reduce.tac b/taccle/examples/lalr_reduce_reduce.tac new file mode 100644 index 0000000..fb7e493 --- /dev/null +++ b/taccle/examples/lalr_reduce_reduce.tac @@ -0,0 +1,18 @@ +# $Id: lalr_reduce_reduce.tac,v 1.1 2004/08/18 23:53:42 tang Exp $ + +# Below illustrates a grammar that is ambiguous by an LALR(1) parser +# but not an LR(1). There is a reduce/reduce conflict given a viable +# prefix ac. taccle resolves this by giving precedence to the first +# listed rule (A -> c). +# +# Reference: +# Dragon book, page 238 + +%token a b c d e + +%% + +S: a A d | b B d | a B e | b A e ; + +A: c ; +B: c ; diff --git a/taccle/examples/reduce_reduce.tac b/taccle/examples/reduce_reduce.tac new file mode 100644 index 0000000..4de6556 --- /dev/null +++ b/taccle/examples/reduce_reduce.tac @@ -0,0 +1,20 @@ +# $Id: reduce_reduce.tac,v 1.1 2004/08/18 23:53:43 tang Exp $ + +# This is an example of where a lookahead of just one symbol is +# insufficient. taccle finds a reduce/reduce conflict and resolves it +# by giving precedence to the first rule (cart_animal -> horse). +# +# Reference: +# lex & yacc, page 55 + +%token and cart plow horse goat ox + +%% + +phrase: cart_animal and cart + | work_animal and plow + ; + +cart_animal: horse | goat ; + +work_animal: horse | ox ; diff --git a/taccle/examples/reduce_reduce2.tac b/taccle/examples/reduce_reduce2.tac new file mode 100644 index 0000000..53c6d3b --- /dev/null +++ b/taccle/examples/reduce_reduce2.tac @@ -0,0 +1,15 @@ +# $Id: reduce_reduce2.tac,v 1.1 2004/08/18 23:53:43 tang Exp $ + +# Here is another reduce/reduce conflict. +# +# Reference: +# lex & yacc, page 225 + +%token A B C Z + +%% + +start: A B x Z | y Z; + +x: C; +y: A B C; diff --git a/taccle/examples/shift_reduce.tac b/taccle/examples/shift_reduce.tac new file mode 100644 index 0000000..8905e26 --- /dev/null +++ b/taccle/examples/shift_reduce.tac @@ -0,0 +1,17 @@ +# $Id: shift_reduce.tac,v 1.1 2004/08/18 23:53:43 tang Exp $ + +# This is an example of a shift/reduce conflict. Eventually I will +# add operator precedence and associativity, but for now taccle simply +# gives higher precedence to shifts. +# +# Reference: +# lex & yacc, pages 229-230 and 236 + +%token TERMINAL + +%% + +expr: TERMINAL + | expr '+' expr + | expr '-' expr + | expr '*' expr ; diff --git a/taccle/examples/shift_reduce2.tac b/taccle/examples/shift_reduce2.tac new file mode 100644 index 0000000..c3ad0d3 --- /dev/null +++ b/taccle/examples/shift_reduce2.tac @@ -0,0 +1,20 @@ +# $Id: shift_reduce2.tac,v 1.1 2004/08/18 23:53:43 tang Exp $ + +# Here is one final shift/reduce conflict. taccles resolves the +# problem by giving precedence to a shift. +# +# Reference: +# lex & yacc, pages 226-227 + +%token A R + +%% + +start: x1 + | x2 + | y R; + +x1: A R; +x2: A z; +y: A; +z: R; diff --git a/taccle/examples/simple_calculator.tac b/taccle/examples/simple_calculator.tac new file mode 100644 index 0000000..ea104aa --- /dev/null +++ b/taccle/examples/simple_calculator.tac @@ -0,0 +1,36 @@ +# $Id: simple_calculator.tac,v 1.1 2004/08/18 23:53:43 tang Exp $ + +# This example demonstrates symbol and synthesized values. + +%{ +#!/usr/bin/tclsh + +%} + +%token ID NEWLINE +%start start + +%% + +start: E NEWLINE { puts "Result is $1" } + | E { puts "Result is $1" } + ; + +E: E '+' T { set _ [expr {$1 + $3}] } + | E '-' T { set _ [expr {$1 - $3}] } + | T + ; + +T: T '*' F { set _ [expr {$1 * $3}] } + | T '/' F { set _ [expr {$1 / $3}] } + | F + ; + +F: '(' E ')' { set _ $2 } + | ID { set _ $::yylval } + ; + +%% + +source simple_scanner.tcl +yyparse diff --git a/taccle/examples/simple_expressions.tac b/taccle/examples/simple_expressions.tac new file mode 100644 index 0000000..7cc1f58 --- /dev/null +++ b/taccle/examples/simple_expressions.tac @@ -0,0 +1,30 @@ +# $Id: simple_expressions.tac,v 1.1 2004/08/18 23:53:43 tang Exp $ + +# This examples takes simple_grammar and adds actions to each rule. + +%{ +#!/usr/bin/tclsh + +%} + +%token ID +%start E + +%% + +E: E '+' T { puts "E + T" } + | T { puts "T" } + ; + +T: T '*' F { puts "T * F" } + | F { puts "F" } + ; + +F: '(' E ')' { puts "(E)" } + | ID { puts "id" } + ; + +%% + +source simple_scanner.tcl +yyparse diff --git a/taccle/examples/simple_grammar.tac b/taccle/examples/simple_grammar.tac new file mode 100644 index 0000000..ea354d9 --- /dev/null +++ b/taccle/examples/simple_grammar.tac @@ -0,0 +1,27 @@ +# $Id: simple_grammar.tac,v 1.1 2004/08/18 23:53:43 tang Exp $ + +%{ + +source simple_scanner.tcl + +%} + +%token ID + +%% + +E: E '+' T + | T + ; + +T: T '*' F + | F + ; + +F: '(' E ')' + | ID + ; + +%% + +yyparse diff --git a/taccle/examples/simple_scanner.fcl b/taccle/examples/simple_scanner.fcl new file mode 100644 index 0000000..99ff67f --- /dev/null +++ b/taccle/examples/simple_scanner.fcl @@ -0,0 +1,16 @@ +# $Id: simple_scanner.fcl,v 1.2 2004/08/18 23:55:31 tang Exp $ + +%{ +source "simple_calculator.tab.tcl" +%} + +%option interactive + +number [0-9]+ + +%% + +{number} { set ::yylval $yytext; return $::ID } +\n { return $::NEWLINE } +\s # ignore whitespace +. { set ::yylval $yytext; return $yytext } |