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