summaryrefslogtreecommitdiff
path: root/examples/manual/cat.lex
diff options
context:
space:
mode:
Diffstat (limited to 'examples/manual/cat.lex')
-rw-r--r--examples/manual/cat.lex45
1 files changed, 45 insertions, 0 deletions
diff --git a/examples/manual/cat.lex b/examples/manual/cat.lex
new file mode 100644
index 0000000..7890aa2
--- /dev/null
+++ b/examples/manual/cat.lex
@@ -0,0 +1,45 @@
+/*
+ * cat.lex: A demonstration of YY_NEW_FILE.
+ */
+
+%{
+#include <stdio.h>
+
+char **names = NULL;
+int current = 1;
+%}
+
+%%
+<<EOF>> {
+ current += 1;
+ if(names[current] != NULL){
+ yyin = fopen(names[current],"r");
+ if(yyin == NULL){
+ fprintf(stderr,"cat: unable to open %s\n",
+ names[current]);
+ yyterminate();
+ }
+ YY_NEW_FILE;
+ } else {
+ yyterminate();
+ }
+ }
+%%
+
+int main(int argc, char **argv)
+{
+ if(argc < 2){
+ fprintf(stderr,"Usage: cat files....\n");
+ exit(1);
+ }
+ names = argv;
+
+ yyin = fopen(names[current],"r");
+ if(yyin == NULL){
+ fprintf(stderr,"cat: unable to open %s\n",
+ names[current]);
+ yyterminate();
+ }
+
+ yylex();
+}