1*357f1050SThomas Veerman /*
2*357f1050SThomas Veerman  * This file is part of flex.
3*357f1050SThomas Veerman  *
4*357f1050SThomas Veerman  * Redistribution and use in source and binary forms, with or without
5*357f1050SThomas Veerman  * modification, are permitted provided that the following conditions
6*357f1050SThomas Veerman  * are met:
7*357f1050SThomas Veerman  *
8*357f1050SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
9*357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
10*357f1050SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
11*357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
12*357f1050SThomas Veerman  *    documentation and/or other materials provided with the distribution.
13*357f1050SThomas Veerman  *
14*357f1050SThomas Veerman  * Neither the name of the University nor the names of its contributors
15*357f1050SThomas Veerman  * may be used to endorse or promote products derived from this software
16*357f1050SThomas Veerman  * without specific prior written permission.
17*357f1050SThomas Veerman  *
18*357f1050SThomas Veerman  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19*357f1050SThomas Veerman  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20*357f1050SThomas Veerman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*357f1050SThomas Veerman  * PURPOSE.
22*357f1050SThomas Veerman  */
23*357f1050SThomas Veerman 
24*357f1050SThomas Veerman %{
25*357f1050SThomas Veerman /* A template scanner file to build "scanner.c".
26*357f1050SThomas Veerman    Run as:
27*357f1050SThomas Veerman           test-lineno-r    # report flex's yylineno
28*357f1050SThomas Veerman           test-lineno-r 1  # report count_newlines(stdin)
29*357f1050SThomas Veerman */
30*357f1050SThomas Veerman 
31*357f1050SThomas Veerman #include <stdio.h>
32*357f1050SThomas Veerman #include <stdlib.h>
33*357f1050SThomas Veerman #include "config.h"
34*357f1050SThomas Veerman 
35*357f1050SThomas Veerman %}
36*357f1050SThomas Veerman 
37*357f1050SThomas Veerman %option 8bit outfile="scanner.c" prefix="test"
38*357f1050SThomas Veerman %option nounput nomain noyywrap yylineno reentrant
39*357f1050SThomas Veerman %option warn
40*357f1050SThomas Veerman 
41*357f1050SThomas Veerman WORD [[:alpha:]]+
42*357f1050SThomas Veerman DIGIT [[:digit:]]
43*357f1050SThomas Veerman 
44*357f1050SThomas Veerman %%
45*357f1050SThomas Veerman     /* The goal here is to test the yylineno processing by:
46*357f1050SThomas Veerman        - providing some rules than CAN match newlines and
47*357f1050SThomas Veerman          other rules that can NOT match newlines,
48*357f1050SThomas Veerman        - matching several newlines in one rule,
49*357f1050SThomas Veerman        - directly modifying yylineno.
50*357f1050SThomas Veerman     */
51*357f1050SThomas Veerman 
52*357f1050SThomas Veerman "yylineno++"          yylineno++;
53*357f1050SThomas Veerman "yylineno--"          yylineno--;
54*357f1050SThomas Veerman [[:blank:]]+
55*357f1050SThomas Veerman {WORD}
56*357f1050SThomas Veerman {DIGIT}+(\n{DIGIT}+)*
57*357f1050SThomas Veerman \n
58*357f1050SThomas Veerman .
59*357f1050SThomas Veerman <<EOF>>  { printf("%d\n", yylineno);
60*357f1050SThomas Veerman            yyterminate();
61*357f1050SThomas Veerman          }
62*357f1050SThomas Veerman 
63*357f1050SThomas Veerman %%
64*357f1050SThomas Veerman 
65*357f1050SThomas Veerman /* returns number of '\n' characters in input, plus one.
66*357f1050SThomas Veerman    This is what flex does, essentially. */
67*357f1050SThomas Veerman 
68*357f1050SThomas Veerman static int
69*357f1050SThomas Veerman count_newlines (FILE* in)
70*357f1050SThomas Veerman {
71*357f1050SThomas Veerman     int n=1,c;
72*357f1050SThomas Veerman     while ((c=fgetc(in)) != EOF)
73*357f1050SThomas Veerman         if( c == '\n')
74*357f1050SThomas Veerman             n++;
75*357f1050SThomas Veerman     return n;
76*357f1050SThomas Veerman }
77*357f1050SThomas Veerman 
78*357f1050SThomas Veerman int main ( int argc, char** argv );
79*357f1050SThomas Veerman 
80*357f1050SThomas Veerman int
main(argc,argv)81*357f1050SThomas Veerman main (argc, argv)
82*357f1050SThomas Veerman     int argc;
83*357f1050SThomas Veerman     char ** argv;
84*357f1050SThomas Veerman {
85*357f1050SThomas Veerman     if( argc > 1 )
86*357f1050SThomas Veerman         printf("%d\n", count_newlines(stdin));
87*357f1050SThomas Veerman 
88*357f1050SThomas Veerman     else{
89*357f1050SThomas Veerman         yyscan_t s;
90*357f1050SThomas Veerman         yylex_init(&s);
91*357f1050SThomas Veerman         yyset_in(stdin,s);
92*357f1050SThomas Veerman         yyset_out(stdout,s);
93*357f1050SThomas Veerman         yylex(s);
94*357f1050SThomas Veerman         yylex_destroy(s);
95*357f1050SThomas Veerman     }
96*357f1050SThomas Veerman     return 0;
97*357f1050SThomas Veerman }
98