delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/29/14:13:35

Message-ID: <327643AC.3DA2@pobox.oleane.com>
Date: Tue, 29 Oct 1996 18:49:32 +0100
From: Francois Charton <deef AT pobox DOT oleane DOT com>
Organization: CCMSA
MIME-Version: 1.0
To: Dean <deanh AT zip DOT com DOT au>
CC: djgpp AT delorie DOT com
Subject: Re: Newbie HELP!!! - Reading input files
References: <54njsl$v7c AT the-fly DOT zip DOT com DOT au>

Dean wrote:
> 
> I'm reasonably new to C/C++, and my question is this:
> 
> I'm trying to write a program which uses input files that are similar to
> those in POVRAY ie;
> 
> object {
>   ...
> }
> 
> Now, to read this in and for my program to understand it, do I have to
> scan through every character and try work out what is happening, or is there
> an easier way to decide what is currently being read?
> 
> Any help would be appresiated...
> 

Hmmm... this is certainly not a djgpp specific question... but anyway...

What you are trying to do is usually called "parsing", DJGPP includes two 
utilities to generate such parsers, called FLEX and BISON (files 
flx252b.zip and bsn124b.zip).

Both are given a description of your language as an input, and produce a 
working C program as an output: you tell them what you want the parser to 
do, and it produces C code which does just that, ain't it great!

In fact, the part of GCC which reads your C files has been developped 
with these.

FLEX produces "lexical analysers" : things which read words (like names, 
numbers and the like) and return "Tokens" (some C structure containing 
information on them, like their names, types and the like). 

BISON produces "parsers", which get "tokens" produced by lexical 
analysers, and group them into "sentences" (objects, whatever) and take 
"actions". 

As an example, take a pocket calculator, the lexical analyser 
would read the figures and symbols you input, and the parser would 
process the operations and calculate the result.

To learn about using them, download the packages and read the info files 
in them... I suggest you start with Bison, very few applications actually 
need Flex.

An excellent discussion on parsers and the way to develop them can be 
found in "Principles of Compiler Design" (or a similar name), by Aho, 
Ullmann and Sethi, McGrawHill (aka "the red dragon book" because of the 
drawing on its cover).


Of course, this is the hard way to proceed: you'll suffer but you'll 
learn a lot, and you will be able to implement very flexible parsers for 
complex languages...

If your language is very simple though: you can try to look at an example 
of "handcoded parser": povray is a good, albeit big, example, look at the 
files tokenize.c (lexical an.) and parse.c.

Hope this helps,
Francois

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019