Incomplete last line won't match

Francis Harvey <>
14 Jun 2001 00:25:52 -0400

          From comp.compilers

Related articles
Incomplete last line won't match (Francis Harvey) (2001-06-14)
Re: Incomplete last line won't match (Tim Van Holder) (2001-06-21)
Re: Incomplete last line won't match (Zhu Wenzhang) (2001-06-21)
| List of all articles for this month |

From: Francis Harvey <>
Newsgroups: comp.compilers
Date: 14 Jun 2001 00:25:52 -0400
Organization: Compilers Central
Keywords: lex, question
Posted-Date: 14 Jun 2001 00:25:52 EDT


Using Flex 2.5.4 and Berkeley Yacc, I have finished a fairly complete
program for analyzing the syntax and logic of a user's source file.
Unfortunately, my program has one glaring flaw. If a user provides a
source file whose last line does not have a newline character at the
end, the parser will fail to recognize all of the rules that it should
and the last line ends up getting echoed to my output file. All of my
other rules and grammar will be fulfilled, but for the last line only
the initial condition rule (.*\n?) will be matched and then no other.
If I simply add a newline to the last line of the file, the code then
works, but I can't seem to get this to work in my program using unput
without getting a buffer error. Any suggestions are most appreciated.

For example, imagine these are the last 3 lines of the source file
where the last line does not have a newline character after the last
letter (testing on the UNIX):

V BL24C 10 A 24 0082-0091

The simplified applicable rules should be:

.*\n? {
                                BEGIN COED;

<COED>^V{ws} {
                                BEGIN V;
                                return VSYM;

<V>{ident}{ws}{vstat} {
                                BEGIN 0;
                                return VSTAT;

<COED>^Q{ws} {
                                BEGIN Q;
                                return QSYM;

<Q>.+\n? {
                                BEGIN 0;
                                return QSTAT;

<COED>^C{ws} {
                                BEGIN C;
                                return CSYM;

<C>(\*{1,20}{ws})?{cval}{1,99}({ws}?-{ws}?{cval}{1,99})?{ws}={ws}.+\n? {
                                BEGIN 0;
                                return CSTAT;

which should close out these simplified grammar statements:

Vgroup : Vstatement Qgroup Cgroup

Vstatement : VSYM VSTAT

Qgroup : /* empty */
                                | Qstatement

Qstatement : QSYM QSTAT

Cgroup : /* empty */
                                | Cgroup Cstatement
                                | Cstatement

Cstatement : CSYM CSTAT
[I'd define my own YY_INPUT that forces a newline at the end of the last
line. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.