[NB: Please don't go sending this sort of stuff to Linus; it isn't
remotely related to the Linux kernel.]

Bill cheng wrote:

> I am studying compiling principle of C programming, but I have no idea
> where I can start with. For example, how can it parse the text input by
> a user?

> To make the problem clearer, I will take an example. Say you have an
> expression, like "(a<1.5) OR (b='abc') AND (2 of (c>0, NOT(d>1),
> (e='cde') AND (f+g*h/i<1))......". (the expression: 2 of (c>0,
> NOT(d>1), (e='cde') AND (f+g*h/i<1) means when 2 expressions in the
> brackets are true, the expression is true). When input by user, those a,
> b, c,d will be replaced by real value, like "(1<1.5) OR ('cde'='abc')
> AND (2 of (1>0, NOT(5>1), ('cde'='cde') AND (4+8*2/3<1))". You must
> compute the result of the expression. Do you know what I mean?

Yes.

> What I want to know is whether you could kindly give me some
> enlightenment. I really do not know what technique to use to parse the
> expression, and how to use some data structure to compute the result,
> (using a linked list, or something else, and also paying attention to
> the priority of the operator.)
> Any real implementation or information resource will be appreciated.

Parsers are generally written using `lex' and `yacc' (or the GNU
equivalents, `flex' and `bison').

Each of these programs reads a grammar definition file as input, and
generates a C source file as output. lex uses a regular grammar to
generate code to convert text into a stream of tokens, and yacc uses a
context-free grammar to generate code to convert a stream of tokens
into a parse tree.

For details, see the Info files for flex and bison. Also, O'Reilly
publish a book on lex and yacc.

Regular and context-free grammars are standard concepts from
computational theory, and are described in many text books.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to