Menu

Tree [r31] /
 History

HTTPS access


File Date Author Commit
 Makefile 2018-07-26 hugoetchegoyen [r31] added Parser class
 README 2018-07-26 hugoetchegoyen [r31] added Parser class
 peg.h 2018-07-26 hugoetchegoyen [r31] added Parser class
 pegparser.h 2018-07-26 hugoetchegoyen [r31] added Parser class
 pegpp.pdf 2018-07-26 hugoetchegoyen [r31] added Parser class
 username.cc 2018-04-12 hugoetchegoyen [r21] Replaced Chr() and Str() by Lit(). Added _() an...
 varcalc.cc 2018-07-26 hugoetchegoyen [r31] added Parser class

Read Me

Examples using the pegplusplus library.

Username
--------

This is a simple example using the facilities defined in peg.h.

It shows how LEG parsers can be easily translated to pegplusplus. 
The LEG version (username.leg) was taken from the PEG/LEG distribution.

The parser replaces all occurrences of the string "username" by the current user's 
login name.

Varcalc
-------

This is a more complex example that encapsulates the parser in a class, using the
Parser class defined in pegparser.h.

Varcalc is a calculator with the following features:

    Values are doubles. Valid input formats: 

        17
        2.
        2.33
        .28
        3.21e3
        22e-5

    Operations supported:

        addition/substraction
        multiplication/division
        exponentiation
        grouping (parentheses)
        variable assignment
        printing (print command)

    Named variables: 

        variable names have the format of C identifiers
        assignment is an expression (assignments can be cascaded)

    Operators from highest to lowest precedence and their associativity:

        ^       exponentiation      left
        + -     unary plus/minus    right
        * /     multiply/divide     left
        + -     add/substract       left
        =       assignment          right

Varcalc reads statements. Newline or semi-colon end a statement.

Statements can be:

    <expression>
    print <expression>

Any text from // to the end of the line is a comment.
White space, empty lines and comments are ignored. 
Invalid statements are reported as errors and ignored. 
Valid statements are executed.

Let's assume the following input:

    a = b = 23 + c
    print 2 * a
    hello!
    print c
    print 2^(-1/2) 

Variable c is being used without a previous assignment. This is valid, the variable is
automatically initialized with value 0 and a warning is printed on standard error.

This is the output:

    line 1: defining c = 0      (standard error)
    46                          (standard output)
    line 3: ERROR: hello!       (standard error)
    0                           (standard output)
    0.707107                    (standard output)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.