summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/grammar_aycock/aycock.test
blob: f3ae1b19724d2083e7b8cc74893b5434c4c0cee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# -*- tcl -*-
# aycock.test --
#
#       Tests for the Aycock-Earley-Horspool parser generator
#
# Tests for the Aycock-Earley-Horspool parser generator are quite rudimentary
# at this point; they walk through only basic functionality and surely do not
# explore corner cases.

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2.0

support {
    useLocal     aycock-runtime.tcl grammar::aycock::runtime grammar::aycock
    useLocalKeep aycock-debug.tcl   grammar::aycock::debug   grammar::aycock
}
testing {
    useLocalKeep aycock-build.tcl   grammar::aycock          grammar::aycock
}

# -------------------------------------------------------------------------

proc parser1 {} {
    grammar::aycock::parser {
        S ::= if E then S else S {
            set _
        }
        S ::= if E then S {
            set _
        }
        S ::= X {}
    }
}

test aycock-1.1 {basic parser for an ambiguous grammar} {
    -body {
        set parser [parser1]
        set result [$parser parse \
                        {if E  then if E  then X  else X } \
                        {if E1 then if E2 then S1 else S2}]
        $parser destroy
        unset parser
        set result
    }
    -cleanup {unset result}
    -result {if E1 then {if E2 then S1 else S2}}
}
test aycock-1.2 {basic parser, another case} {
    -setup {
        set parser [parser1]
    }
    -body {
        $parser parse \
            {if E  then if E  then X  else X  else if E  then X  else X } \
            {if E1 then if E2 then S1 else S2 else if E3 then S3 else S4}
    }
    -cleanup {$parser destroy; unset parser}
    -result {if E1 then {if E2 then S1 else S2} else {if E3 then S3 else S4}}
}

test aycock-2.1 {save and restore a parser} {
    -body {
        set parser1 [parser1]
        set saved [$parser1 save]
        $parser1 destroy
        set parser2 [eval $saved]
        $parser2 parse \
            {if E  then if E  then X  else X } \
            {if E1 then if E2 then S1 else S2}
    }
    -cleanup {
        catch {$parser2 destroy}
        catch {unset parser2}
        catch {unset saved}
        catch {$parser1 destroy}
        catch {unset parser1}
    }
    -result {if E1 then {if E2 then S1 else S2}}
}

rename parser1 {}

test aycock-3.1 {dangling else grammar, another form} {
    -body {
        set parser [grammar::aycock::parser {
            S ::= if E then S elsepart {
                set _
            }
            elsepart ::= else S {
                set _
            }
            elsepart ::= {
                list (empty)
            }
            S ::= X {}
        }]
        list [$parser parse \
		  {if E  then if E  then X  else X } \
		  {if E1 then if E2 then S1 else S2}] \
	    [$parser parse \
		 {if E  then if E  then X  else X  else if E  then X  else X } \
		 {if E1 then if E2 then S1 else S2 else if E3 then S3 else S4}]
    }
    -cleanup {
        catch {$parser destroy}
        catch {unset parser}
    }
    -result {{if E1 then {if E2 then S1 (empty)} {else S2}} {if E1 then {if E2 then S1 {else S2}} {else {if E3 then S3 {else S4}}}}}
}

test aycock-3.2 {unary and binary operations, wrong precedence} {
    -body {
	set parser [grammar::aycock::parser {
	    E ::= E - E {set _}
	    E ::= E + E {set _}
	    E ::= UMINUS E {set _}
	    E ::= X {set _}
	    UMINUS ::= - {list UMINUS}
	}]
	list \
	    [$parser parse \
		 {- X - X} \
		 {- a - b}] \
	    [$parser parse \
		 {X - X - X} \
		 {a - b - c}] \
	    [$parser parse \
		 {X + X - X} \
		 {a + b - c}]
    }
    -cleanup {
	catch {$parser destroy}
	catch {unset parser}
    }
    -result {{UMINUS {a - b}} {{a - b} - c} {{a + b} - c}}
}

test aycock-4.1 {parses with lots of ambiguity} {
    -body {
	set parser [grammar::aycock::parser {
	    A ::= b B {set _}
	    B ::= P P Q {linsert $_ 0 rule1}
	    B ::= P Q Q {linsert $_ 0 rule2}
	    P ::= p {}
	    P ::= {list empty P}
	    Q ::= q {}
	    Q ::= {list empty Q}
	}]
	list \
	    [$parser parse {b} {b}] \
	    [$parser parse {b p q} {b p q}] \
	    [$parser parse {b q q} {b q q}]
    }
    -cleanup {
	catch {$parser destroy}
	catch {unset parser}
    }
    -result {{b {rule1 {empty P} {empty P} {empty Q}}} {b {rule1 {empty P} p q}} {b {rule2 {empty P} q q}}}
}

test aycock-5.1 {desk calculator skeleton} {
    -body {
	set p [grammar::aycock::parser {
	    start ::= E {}
	    E ::= E + T {expr {[lindex $_ 0] + [lindex $_ 2]}}
	    E ::= T {}
	    T ::= T * F {expr {[lindex $_ 0] * [lindex $_ 2]}}
	    T ::= F {}
	    F ::= NUMBER {}
	    F ::= ( E ) {lindex $_ 1}
	}]
	list \
	    [$p parse \
		 {NUMBER * NUMBER + NUMBER} \
		 {2      * 3      + 4     }] \
	    [$p parse \
		 {NUMBER * ( NUMBER + NUMBER )} \
		 {2      * ( 3      + 4      )}]
    }
    -cleanup {
	catch {$p destroy}
	catch {unset p}
    }
    -result {10 14}
}

# -------------------------------------------------------------------------

tcltest::cleanupTests
return