summaryrefslogtreecommitdiff
path: root/test/counting4.lm
diff options
context:
space:
mode:
Diffstat (limited to 'test/counting4.lm')
-rw-r--r--test/counting4.lm89
1 files changed, 89 insertions, 0 deletions
diff --git a/test/counting4.lm b/test/counting4.lm
new file mode 100644
index 00000000..b1a75130
--- /dev/null
+++ b/test/counting4.lm
@@ -0,0 +1,89 @@
+
+#
+# Regular Definitions
+#
+rl rl_ws /[ \t\n\r\v]+/
+rl rl_id /[a-zA-Z_][a-zA-Z0-9_]*/
+rl rl_num /[0-9]+/
+
+#
+# Tokens
+#
+
+lex start
+{
+ # Ignore whitespace.
+ ignore /rl_ws/
+
+ literal ';'
+
+ # Tokens.
+ token id /rl_id/
+ token number /rl_num/
+}
+
+#
+# Global Data
+#
+
+global int target
+global int count
+
+#
+# Productions
+#
+
+
+def get_target
+ [number]
+ {
+ count = 0
+ target = r1.data.atoi()
+ print( 'target: ', target, '\n' )
+ }
+
+# Arbitrary item.
+def item
+ [number]
+| [id]
+
+def count_items
+ [count_inc item count_items]
+| [count_end]
+
+def count_inc
+ []
+ {
+ if count < target
+ count = count + 1
+ else
+ reject
+ }
+
+def count_end
+ []
+ {
+ if count < target
+ reject
+ }
+
+def counted_list
+ [get_target count_items]
+
+def start
+ [counted_list*]
+ {
+ for List:counted_list in lhs {
+ match List [Count:number Items:count_items]
+ print( 'num items: ', Count.data.atoi(), '\n' )
+
+ int i = 1
+ for Item:item in Items {
+ print( ' item ', i, ': ', Item, '\n' )
+ i = i + 1
+ }
+ }
+ print( '*** SUCCESS ***\n' )
+ }
+
+parse start(stdin)