🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2445

Re: Looking for recursive decent C parser

#2445
From: Kazimir Kylheku
Date: Mon, 01 Apr 1996 13:19
47 lines
2255 bytes
In article <4io1jt$nph@solutions.solon.com>,
Colin Fry <colin@tacos.caltech.edu> wrote:
>  If anyone has implemented a recursive decent C (or subset of C) parser,
>and would supply same please contact colin@tacos.caltech.edu. I have a YACC
>implementation of an interpreted C subset, but the users don't like the
>uninformative error messages. 

If that is your only problem, why not fix it with error productions? Yacc
parsers can be made quite friendly in reporting error messages. Don't give up
right away---someone else's recursive descent parser might be harder to
incorporate into your project, and more difficult to subsquently maintain, than
augmenting your existing one with more meaningful error productions.

You might also want to investigate the facilities of the GNU Bison parser
generator.



An error production is one which contains the special yacc keyword ``error''.

for example,

foo_declaration
	: T_VAR T_INTEGER ':' varlist '.'
	| T_VAR T_REAL ':' varlist '.'
	| T_VAR T_INTEGER error '.'
	{
		errorprint("colon expected after `integer'");
	}
	| T_VAR error '.'
	{
		errorprint("unknown variable type!\n");
	}
	;

This first error production means that if an error is encountered in parsing the
foo_declaration after the parser has seen the T_INTEGER keyword, it will detect
a syntax error, then keep reading input symbols until it sees a terminating '.'
after which it will reduce by the error production and do the action.

The second error production will kick in of the T_VAR keyword symbol is
immediately followed by a parse error, which means that it was not followed by
T_INTEGER or T_REAL.

[warning: yacc is not C, and further discussion of Yacc should probably
 go to another newsgroup. -mod]
-- 
Technical Details
Message-ID: <4jol5e$1q0@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4io1jt$nph@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
Newsgroups: comp.lang.c.moderated
Subject: Re: Looking for recursive decent C parser
Date: 1 Apr 1996 07:19:42 -0600
Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4jol5e$1q0@solutions.solon.com>
References: <4io1jt$nph@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com