Plisp: Lisp/S-expression parser library

Plisp is a C library for parsing Lisp/S-expressions. I wrote it because I got massively sick of writing the same code every time I wanted to parse a text file.
I'd like some testers/users for this. It suits *my* needs, but I'm not too good at accommodating other people's. Do give it a try.
Installation:
I decided to mix it up and use Mercurial instead of Git. I'm liking the change.
AUR package is here: http://aur.archlinux.org/packages.php?ID=35422
hg clone URI (also web view): http://peasantoid.org:1024/plisp
Manual installation:
$ root=/path/to/root_directory ./make install
Documentation:
Sorely lacking. I will attempt to write some of this later; in the meantime, look at the source (particularly header files) and everything in test/.
Technical details:
Text is processed in two stages. The first, tokenization, iterates through the text looking for 'simple' tokens (left/right parentheses, quotes, numbers, strings, symbols, etc.). The second, parsing, iterates through the token list and converts it to a parse tree.
POSIX ERE (extended regular expressions) are used for tokenization. Seriously, this is really handy. $(man 3 regex) for details.
Linked lists are heavily relied upon.
Note that numbers are just stored as strings. It's up to the programmer to change those to actual numbers (f.e. strtol(), strtod()).
Testing:
The distribution includes a simple syntax-dumper utility that takes data from stdin, parses it, and formats the resulting syntax tree to stdout. My terminal transcript follows:
narch plisp -> ./make test && build/test/dump
---[RUN test (lib)]
---[RUN lib (obj)]
---[RUN obj ()]
build/obj/error.o
build/obj/escape.o
build/obj/init.o
build/obj/list.o
build/obj/parser.o
build/obj/tokenizer.o
---[END obj]
build/libplisp.so
build/libplisp.a
build/include/*.h
---[END lib]
build/test/dump
---[END test]
(foo '("bar") `(3.456 ,(baz 4 5)))
^D
1:1 list: ...
1:2 symbol: foo
1:7 list (quoting: '): ...
1:8 string: bar
1:16 list (quoting: `): ...
1:17 real: 3.456
1:20 list (quoting: ,): ...
1:21 symbol: baz
1:25 integer: 4
1:27 integer: 5
Tested by/on:
me: Arch 64-bit (x86_64 Intel Core 2 Duo (Apple MacBook3,1))
me: Arch 64-bit (x86_64 Intel Xeon (Xen))
me: ArchPPC (32-bit PowerPC processor (Apple Mac Mini G4))
If you test this, please post your architecture and processor type. I really hate those insidious system-specific bugs.
Last edited by Peasantoid (2010-03-13 05:31:46)

Peasantoid wrote:Interesting thing I discovered: you have to pass '-lm' to GCC in order to get it to link with the math functions. Does anyone know why this isn't in libc?
I've encountered this before, it's one of those things that make you go "huh?". Nice work on getting the math module done
As a suggestion, it would be useful to have a module which allows loading and running arbitrary functions from shared libraries, although I'm not too sure where to start with implementing this, I think there is a library around that allows you to do this fairly easily, but I can't seem to find it right now. But as a relatively new scripting language something like this would make it easier to interact to common libraries while other modules are still being written.
Edit: Obviously I was searching for the wrong thing, what I'm talking about is a foreign function interface
Last edited by HashBox (2009-06-13 01:43:44)

Similar Messages

  • Xerces C++ Parser Library

    Hello everybody. I want to learn two simple issues. If you answer, it will be very nice of you.
    1) I try to develop an application on a Sun workstation having a sparc processor. It runs SunOS 5.9. The IDE that I use is Sun ONE Studio 4 update 1 Enterprise Edition for Java (Build 020923) IDE/1
    spec=1.43.3.1 impl=020923. I use C++ programming language (with the relevant Compiler Collection of Sun). In order to parse XML files, I plan to use Xerces C++ Parser Library. Apache web site says that this library has been developed with Forte C++ Version 6 Update 2 on Solaris 2.7. Will I encounter any incompatibility problems?
    2) Which distribution of Xerces-C++ must I download? Binary or source...
    3) Apache web site ( http://xml.apache.org/xerces-c/build-winunix.html#UNIX ) says that "Xerces-C++ uses GNU tools like Autoconf and GNU Make to build the system." These tools are used to generate a Makefile to build a XML parser executable. But, Sun One Studio already has a tool to build Makefile. Do I really need to use those GNU tools?
    4) I've tried to build a Makefile for Xerces C++ applications. During building Makefile in "Directories to search for include files" window, I've added the directories which reside under /include/xercesc. In "Libraries (filenames and directories) to link with" window, I've added the files residing under lib directory which are libxerces-c.so, libxerces-c.so.25 and libxerces-c.so.25.0. But, at compile time I encounter errors, since I couldn't include the relevant .hpp files successfully.
    Thanks in advance & Regards

    The error message file lpxus.msg has a detailed explanation:
    00252, 00000, "invalid entity replacement-text nesting"
    // *Cause: Markup included from an entity must nest/group properly, that
    // is, open/close markup must occur within the same entity.
    // For example,
    // <!DOCTYPE doc [ <!ENTITY e "</foo><foo>"> ]>
    // <doc><foo>&e;</foo></doc>
    // Is invalid since foo's start-tag occurs in the top-level
    // document, but the close-tag is provided by the "e" entity.
    // Both start and end must be provided by the same source.
    // *Action: Stay away from tricky nonsense such as the above, it's
    // not permitted.
    null

  • Template Expressions in Library Items?

    Hi All,
    The quick question is this: can template expressions be made
    to work inside library items?
    Here’s the situation…
    The page:
    http://www.fretbank.com/basics/intervals/index.html
    I have a 3-tier template structure in place – 1-Root,
    2-General Page Structure, 3-Topic Categories.
    If you look at the page, I have buttons below the header that
    point to different topics. Each topic has several pages. All of the
    pages from each topic are child-pages from a specific Tier-3
    template.
    The buttons use template parameters/expressions on the Tier 3
    temp. to determine the state of the button.
    I currently have to copy the code for the button nav into
    each topics template and any changes have to be made in each
    template. I am going to be adding many more topics and want to find
    a shortcut. I guess I could make a 4th template tier and have each
    of the category templates be children of that, but I’m hoping
    to avoid this. It would be easiest to be able to make a library
    item and paste it where I need.
    I thought about using SSI’s but that wouldn’t
    work with the expressions. Any other suggestions?
    Thanks,
    Troy
    here's the code if it helps:

    > Is having the link outside the div a problem in older
    browsers?
    It's invalid HTML. That's enough for me. And I suspect it
    would be a
    problem somewhere, as you are trying to make the entire
    <div> tag a link.
    > This allows me to only have to change each of these nav.
    elements on one
    > page.
    I would opt for a single template structured so that changes
    to CSS could
    'specify' the particular section layout. Then I would choose
    server-side
    includes for the navigation elements, and place them into
    strategically
    placed editable regions of the single template, depending on
    which section
    you are needing to navigate.
    Having only a single template is not only possible, it's MUCH
    simpler than
    nesting them.
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    ==================
    "Fret Man" <[email protected]> wrote in
    message
    news:[email protected]...
    > Thanks for the reply Murray.
    >
    > A couple clarifications and questions. Sorry for the
    length, but I felt it
    > was
    > better to be clear than concise.
    >
    > I am using nested templates.
    >
    > Since I posted the topic, I have changed my structure a
    bit, trying to
    > find a
    > balance between flexibility and work load.
    >
    > I have a root template with has headers and common
    includes.
    >
    > From the root template I nest multiple MAIN CATEGORY
    templates for primary
    > areas of the site. These templates contain the nav
    buttons just below the
    > header.
    >
    > From the MAIN CATEGORY temps I nest several SUB CATEGORY
    templates for
    > each
    > topic in the category, which contain the code for the
    "Quick Nav" box at
    > the
    > bottom of the page.
    >
    > The individual pages of the site are then built from the
    sub category
    > pages.
    > The reason for all of the nesting is this: The MAIN
    CATEGORY temps contain
    > the
    > code for the nav buttons and are built using temp.
    expressions that look
    > to a
    > main category param on each of the sub category pages.
    Depending on the
    > param., the button that corresonds to which Main
    Category you are in will
    > be
    > black (via CSS class)
    >
    > One the sub cat. temps the Quick nav link relative to
    which page you are
    > on
    > will be black (also via CSS class)
    >
    > This allows me to only have to change each of these nav.
    elements on one
    > page.
    > All of the different MAIN CAT. temps allow me to focus
    advertising based
    > on
    > the subject. Maybe there is a better way to populate the
    ads to different
    > categories that I am not aware of? Is there a way to
    have the button/links
    > reflect the category/page you are on using only CSS? I
    don't want to have
    > to
    > copy and paste changes to multiple pages when the
    nav/topics change.
    >
    > ****
    > I put the <a> tags inside the <div> tags,
    based on multiple people telling
    > me
    > that I shouldn't do it the other way around. I hadn't
    encountered any
    > problems
    > and liked that the whole button became a link. Is having
    the link outside
    > the
    > div a problem in older browsers? I tested it this way in
    IE7, Firefox2.0,
    > Safari, and Opera, and it worked.
    >
    >

  • Amplitube 2 Live and Logic Express 9 Library

    OK so I saw a ton of old threads about how amplitube 2 does not install or authorize or validate on Mac. I was able to successfully install it, authorize it, and validate it through the AU Manager in Logic Express 9. I see all the amp modeling effects in the IK Multimedia folder on my hard drive. However, how do I choose an amp/effect within Logic Express 9? I see that in the Library, it lists all of Logic's presets and even Garage Band's...but not Amplitube's?  I have used this very same installation disk on a PC with Acid Pro a couple of years ago and it worked perfectly. I admit I am still learning the ins and outs of Logic Express.

    1) Create a new audio track.
    2) In the channel strip on the left, click on Inserts>Audio Units>IKMultimedia>Amplitube2Live.
    The Amplitube plugin window will then pop up and allow you to select your rig and tweak your sound.

  • Boolean Expression Parsing

    Hi
    i have a String like "true && !true && (false || true)" and want to know if the expression is true or false. Is there any Helperclass that parses an boolean expression??
    String expression = "true && !true && (false || true)";
    boolean result = BooleanParser.parse(expression );
    Regard,
    Harald

    thats not cheating, just the simplest way to solvethe problem ;)
    ... and one missed opportunity to learn something
    about parser generators.I think there is something to be said for taking classes on that particular subject.
    That particular study area is well defined in computer science is well defined. So the good ideas that one can use are already there just waiting to be learned.

  • XML Parser Library?

    Hi, everybody?
    Environment is :
    Solaris9
    Sun Studio 11
    Dev. Lang : C++
    What is the purpose of this package ?
    Is it xml parser?
    $ pkginfo -l SUNWlxml
    PKGINST: SUNWlxml
    NAME: The XML library
    CATEGORY: system
    ARCH: sparc
    VERSION: 11.9.0,REV=2002.03.02.00.35
    BASEDIR: /
    VENDOR: Sun Microsystems, Inc.
    DESC: The XML library (libxml2-2.4.23)
    PSTAMP: sfw8120020302003617
    INSTDATE: Jan 30 2007 01:12
    HOTLINE: Please contact your local service provider
    STATUS: completely installed
    FILES: 49 installed pathnames
    5 shared pathnames
    7 directories
    4 executables
    2417 blocks used (approx)
    Thnak you.

    Thank you for your reply.
    I want to parse xml files with Sun Studio C++ in Solaris9
    If the SUWNlxml package was installed, is it possible?
    If it is not, Should I install another software separately?
    ---Example of xml file---
    <?xml version="1.0" encoding="UTF-8"?>
    <root name="Books">
    <no="1"/>
    <name="Economic"/>
    </root>
    Thank you.

  • Regular expression - parse version number string

    Hi,
    I try to parse a string using regular expressions but id did not work correctly in all cases.
    The string that i want to parse can have one of the following layout:
    3.4.5.v20090305 or
    3.4.5The first three parts have to be integer values, the last part can be a free string (which is optional).
    I use the following code to parse the version number and extract the parts:
    Pattern versionPattern = Pattern.compile("(\\d+)\\.{1}(\\d+)\\.{1}(\\d+)(?:\\.{1}(\\w+))?");
    Matcher m = versionPattern.matcher(versionString);
    if (!m.find()) {
        throw new IllegalArgumentException("Version must be in form <major>.<minor>.<micro>.<qualifier>");
    // assert that we matched every part
    // three groups (without qualifier) or four parts (with qualifier)
    int groups = m.groupCount();
    if (groups != 4) {
         throw new IllegalArgumentException("Version must be in form <major>.<minor>.<micro>.<qualifier>");
    // extract the parts
    major = parseInt(m.group(1));
    minor = parseInt(m.group(2));
    micro = parseInt(m.group(3));
    qualifier = m.group(4);The above regular expression works in all cases that i tested, except one.
    The follwoing string is accepted as correct input: (but it shouldn't)
    3.4.5a.v20090305And i get the result:
    major = 3
    minor = 4
    micro = 5
    qualifier = a.v20090305
    Thanks for help or suggestions :)
    Best Regards,
    Michael

    heissm wrote:
    Hi,
    I try to parse a string using regular expressions but id did not work correctly in all cases.
    The string that i want to parse can have one of the following layout:
    3.4.5.v20090305 or
    3.4.5The first three parts have to be integer values, the last part can be a free string (which is optional).
    I use the following code to parse the version number and extract the parts:
    Pattern versionPattern = Pattern.compile("(\\d+)\\.{1}(\\d+)\\.{1}(\\d+)(?:\\.{1}(\\w+))?");
    Matcher m = versionPattern.matcher(versionString);
    if (!m.find()) {
    throw new IllegalArgumentException("Version must be in form <major>.<minor>.<micro>.<qualifier>");
    // assert that we matched every part
    // three groups (without qualifier) or four parts (with qualifier)
    int groups = m.groupCount();
    if (groups != 4) {
    throw new IllegalArgumentException("Version must be in form <major>.<minor>.<micro>.<qualifier>");
    // extract the parts
    major = parseInt(m.group(1));
    minor = parseInt(m.group(2));
    micro = parseInt(m.group(3));
    qualifier = m.group(4);The above regular expression works in all cases that i tested, except one.
    The follwoing string is accepted as correct input: (but it shouldn't)
    3.4.5a.v20090305And i get the result:
    major = 3
    minor = 4
    micro = 5
    qualifier = a.v20090305No, that can't be the output. Perhaps you have some old class files you're executing, because with the code you now posted, that can't be the result.
    To verify this, execute this:
    import java.util.regex.*;
    public class Main {
        public static void main(String[] args) {
            String versionString = "3.4.5a.v20090305";
            Pattern versionPattern = Pattern.compile("(\\d+)\\.{1}(\\d+)\\.{1}(\\d+)(?:\\.{1}(\\w+))?");
            Matcher m = versionPattern.matcher(versionString);
            if (!m.find()) {
                throw new IllegalArgumentException("Version must be in form <major>.<minor>.<micro>.<qualifier>");
            // assert that we matched every part
            // three groups (without qualifier) or four parts (with qualifier)
            int groups = m.groupCount();
            if (groups != 4) {
                 throw new IllegalArgumentException("Version must be in form <major>.<minor>.<micro>.<qualifier>");
            // extract the parts
            System.out.println("1 -> "+m.group(1));
            System.out.println("2 -> "+m.group(2));
            System.out.println("3 -> "+m.group(3));
            System.out.println("4 -> "+m.group(4));
    }and you'll see that the following is the result:
    1 -> 3
    2 -> 4
    3 -> 5
    4 -> nullSome remarks about your pattern:
    "(\\d+)\\.{1}(\\d+)\\.{1}(\\d+)(?:\\.{1}(\\w+))?"All the "{1}" can be omitted they don't add anything to your regex and are only cluttering it up. And you'll probably also want to "anchor" your regex using the start- and end-of-string-meta-characters, like this:
    "^(\\d+)\\.(\\d+)\\.(\\d+)(?:\\.(\\w+))?$"

  • Expression parsing and execution

    Hi All,
    I need ideas/code/suggestions from you all. I am working on a Formula Builder UI. In this a user can create their own expressions choosing some variable values also.
    This will contain IF, AND , OR and NOT also from expression creation. For example it may look like this
    IF my_variable > 25 THEN my_variable *20 ELSE my_varibale/2 END
    So these kind of lines should be parsed first for completeness and then should be executed for thier values.
    Hope to get a supportive help from all of you.
    Regards,
    Sandeep

    Hi,
    I am trying to evaluate an expression like this =IF(A1<0.125,0.0005,(IF(A1<0.25,0.00066667,(IF(A1<0.5,0.00075,(IF(A1<0.75,0.000916667,0.0010833))))))) in java. In excel it is easy but i cannot use that. I cannot use any free downloads. Is there any code sample which I can use to start off? I need to parse and evaluate the above expression.

  • Command line arguments C++ parsing library [SOLVED]

    Hi,
    i'm looking for such a library. Requrements:
    1. usage/flag list generaton
    2. support of -s and --long flags
    3. simpliciry of use
    4. support of optional and mandatory flags
    5, support of flagw with/without arguments (-f, -f 123)
    6. code free to use in commerical usage too
    7. C++ (!++!)
    Any suggestions/reccomendations?
    Thanks a lot

    Boost is the de-facto standard for stuff like this.  Take a look at boost::program_options

  • Regular Expression - Parse CSV

    "Q-Accept, Other Employment"
    I need to remove the "," from the above string. The CSV File
    I have includes commas inside quotetaion marks, I need to remove
    the comma and leave the string intact. Any help with te rereplace
    format would help
    Thanks
    Andy

    I think the previous poster may have a typo in the example
    code.
    Try changing this...
    <cfset modifiedString = ReReplace(string, '["]', '',
    'ALL') />
    to this...
    <cfset modifiedString = ReReplace(string, ',', '', 'ALL')
    />
    Good luck!

  • Parsing an Algebraic Expression

    Hi ,
    Can any body provide me an idea to parse an algebraic expression in Java.Are there any built in classes for parsing an expression and evaluating after substituting the values for variables in the expression?
    If no classes are availble,plz provide me any algorithm for doing that.
    Thanks in Advance

    Observe:
    Parsing an expression is essentially the act of converting the expression from infix notation to postfix notation. Infix requires parens and precedence to specify order of operations but post fix unambiguously specifies them by the order of the tokens
    So something like
    "a+b*c" becomes "a b c * +"
    "a*b+c" becomes "a b * c +"
    "(a+b)*c" becomes "a b + c *"
    notice that arguments (variables and numbers, the leaves of the implied expression tree) occur in the same sequence in both the infix and the postfix notation. Thus you can view the parsing as a filtering process that passes arguments directly from input to output and all the filter is doing is re-arranging the order of the operators.
    All this rearranging of operators can be done with a single stack and it is all based on this observation:
    When you saw a string like "a op1 b op2 c" you run across the token for the first operator before you have even collected its right argument. You must put that operator somewhere and a OpStack is the place to put it. You don't know whether to emit op1 until you have had a look at the next operator, op2. If op2 was higher priority than op1, then op2 gets to consume the argument b first. In that case op2 just gets added to the stack on top of op1.
    However if op2 was less than or equal in precedence to op1, well then op1 can now be flowed to the output stream and allowed to act on the b argument that is already out on the output stream.
    This means that the fundamental operation of the expression parser is the business of pushing an opertor token onto the stack, but first spewing to the output all the other operators on the stack that had a higher or equal priority. If we give this function a name like opStack.pushSpew(token) it could look something like this:
    class OpStack{
      Stack s = new Stack();
      void pushSpew(List out, Token t){
        Token tos;
        while(!s.empty() && (tos = peek()).priority >= t.priority){out.add(pop());}
        push(t);
    }Next thing to observe is that a grouper, like open and close parens, behaves in the following way. An open paren acts like a very high priority operator when pushed to the stack in that it spews nothing. The previous op is NOT allowed to eat the argument that is about to come right after the open paren. However once the open paren marks the stack, the open should be a lower priority than any real op that gets pushed onto the stack above it because it must delay going out until every real op in the group had its shot. The close paren should also act like a very low priority op. It should spew every op upto but not including the open paren that started this group.
    As a computation trick, if you set the priority of an open lowest of all, and the priority of a close just above that, and you just push the open onto the stack instead of pushSpewing it onto the stack, you will get the desired behavior. You with then pushSpew the close paren and it will dump everything from the stack to the output up to but not including the open. At this point it is easy to check the balance. If you do not have an open/close pair sitting as the top two elements on the stack then the parens (or the brackets or the braces) were not properly nested.
    We are almost done. There are post fix operators like factorial and prefix operators like minus that we need to deal with. It only makes sense to evaluate these suckers from the inside out i.e. if you had a string of prefixs on an arg
    op1 op2 op3 arg
    the only sensible order is defined by parens like this
    (op1 (op2 (op3 arg) ) )
    and in post fix that would be
    arg op3 op2 op1
    that is exactly what you would get if you just pushed each prefix op onto the stack in the order you saw them and then popped them off after you dumped out the argument.
    Post ops should also work from the inside out
    arg op1 op2
    being
    ( (arg op1) op2 )
    and you get this behavior by just dumping a post op to the output when you see it.
    There is only one special concern. What should you do if you saw, both pre ops and post ops on the same arg?
    is "op1 arg op2" done like this: "(op1 arg) op2" or "op1 (arg op2)"? Well, why should these be any different from regular diadic ops, let precdence decide. An espression like minus three factorial "-3!" should be interpreted as take the factorial of 3 and then negate it (and not the other way around!) so let the priority of factorial be greater than that of minus and just do the standard pushSpew with post ops to let them spew out any high priority pre ops before they go onto the stack, but then unlike binary ops, the post op immediately comes off the stack and goes to the output because post ops bind up right away.
    Now if you are not careful with your precedences you could get "wierd" behavior. If for example you did not assign a high priority to a factorial, you could write expressions like "3+4!" and the priority would treat that like "(3+4)!" the plus would bind tighter than the factorial. It is hard for me to imagine a language where you would want the effects of either a postfix or a prefix operator to span across multiple arguments combined by single diadic operator, but that is really up to you. You certainly could have a language where you gave a very low precedence to ~ which could mean the boolean NOT, a lower precedence than the diadic operator >= which would allow you to write "~ a >= b" which would mean "~(a>=b)"
    After all it is your language, do what you want. None the less I would advise against alowing ANY pre or post op to have a priority LESS than that of ANY diadic operator.
    Lots of talk here but to recap, the code is quite simple:
    while((t = getNextToken()) != null){
      switch (t.type()){
        case ARG:  out.add(t); break; // arguments pass straight to output
        case PRE:  stack.push(t); break; // pre ops are just pushed (binding them to the next arg)
        case OPEN: stack.push(t); break; // Opens just push, they don't spew to output
        case DI: stack.pushSpew(out,t); break; // Diadic ops do the standard pushSpew
        case POST: stack.pushSpew(out,t); out.add(stack.pop()); break; //spew and then go out
        case CLOSE: stack.pushSpew(out,t); stack.removeOCpair(); // spew all ops in group
          // and then check if you had a balancing open
    }That just about does the entire parse. If you can find the tokens in the input you can rearrange them into the output. You do need to flush the opStack when you are done with the input stream and there is a convenient trick to doing that. The trick is this. Invent an invisible Open and Close pair, invisible meaning that they never show up in any input stream, you just made them up and pretended that that expression started with an invisible OpenExp and at the end it closed with a CloseExp.
    The CloseExp at the end will act like any grouper, it will flush the stack looking for the corresponding opener. IF there was any unbalanced grouper, like say an extra unbalanced open paren somewhere in the expression, the CloseExp will spew its way down to that unbalanced paren and then fail when it discovers that it does not match the open in the call to remove the OC pair. The is a way to do error detection. Use a fictional group on the entire expression to both flush the remainder of the opStack and to do a final check for unbalanced groupers.
    In fact, while we are on the topic of error checking there is another one that is very good to include. Basically in a real expression if you ignore the parens, the progression should be an alternation between args and diadic ops, with an occasional pre or post op thrown in.
    A simple count tells you where you are. Go up to 1 when you pass an argument, go back down to zero when you see a diadic operator. You should start at zero, because there have been no arguments or ops yet. In state zero the only legal things to do are to put in a pre op or an arg. Once you have an arg out there the only legal things to do are to have a post op or a diadic op. This little state machine will tell you if and when you have got either a pair of arguments together with no op between them, or a pair of ops with no argument between them. You should start at state = 0 and you should end at state = 1 (unless you allow empty expressions) So we can beef up the error checking with a very simple addition. The guts of the parse will now look like this:
    state = 0
    stack.push(Token.OpenExp); // start off with an open of the expression
    while((t = getNextToken()) != null){
      switch (t.type()){
        case ARG:  assertStateIs(0); out.add(t); state = 1; break;
        case PRE:  assertStateIs(0); stack.push(t); break;
        case OPEN: stack.push(t); break;
        case DI: assertStateIs(1); stack.pushSpew(out,t); state =0; break;
        case POST: assertStateIs(1);stack.pushSpew(out,t); out.add(stack.pop()); break;
        case CLOSE: stack.pushSpew(out,t); stack.removeOCpair();
    stack.pushSpew(out,Token.CloseExp); stack.removeOCpair();
    assertState(1);
    return out;So - there it is, in outline form the way that you parse an expression.You build 3 classes, Token, ExpressionParser, and OpStack
    Token is just a bucket that holds a name, a type (ARG, PRE, OPEN, DI, ...) and a precedence.
    OpStack is just a wrapper for a reqular stack that will hold Tokens and allow for a pushSpew function.
    ExpressionParser is little other than a Map full of all your Tokens. This map allows the parser to map substrings, tokens, that it finds in the input like >= to actual Tokens with a capital T. It will have two main methods:
    1) the parse method which I have almost completly written for you. It takes an input string and will fill up an output list placing the Tokens in post fix order, which is your parse tree.
    2) the routine getNextToken which will walk the input string, skipping white space when appropriate, looking for names, numbers, and operators and building each chunk that it finds into a Token.
    One last complexity note. If you want to allow functional notation like sqrt(3) you are using parens in a fundamentally different way than was described above (ditto when you use brackets to represent array indexing like x[5]) the fundamentally different thing that you are doing is that you are allowing an argument (a name) like "sqrt" to sit right next to another argument like "3" with no binary op in between. You are using an "implied" binary op at that point. You have saved the user from typing "sqrt FUNCTIONCALL (3)" or "x ARRAYINDEX 5"
    I will leave it as an exercise to the reader to see how you make a small modification to the OPEN case to detect and deal with an implied operation. Don't forget that you must tell the state error checker what you are doing.
    And now that I have shown you how fundamentally simple it is to build an expression parser let me comment on why you do not find a standard library routine for doing this.
    The primary reason is that there is no such thing as a "standard" math expression. Parsing is based on a language and there is no one language. Are you going to allow hex numbers in your expressions? How about scientific notation? Is = equality or assignment? Do you allow functional notation? Array indexing? Multi-dimensional array (like x[2,3])? How about string constants like "foobar"? How do you embed quotes in your quoted strings? Are you allowing comments, block comments, statements, code, Lisp expressions, complex numbers, implicit type conversion, type casting? At what point does it stop being a simple expression and start being a full computer language?
    Furthermore, most of the work you will quickly see if you use what I have suggested to build an expression parser, it how to give your user feedback that the expression that he typed had problems and where. There is no stardard UI for error feedback in things that don't parse correctly. Are you just going to dump cryptic strings to the console or are you going to highlight something in a text box somewhere?
    All the work turns out to be that of defining your language and determining your set of tokens and what precedence you will employ. Once you get that all worked out, parsing the expressions only takes the ten or fifteen lines of code that I outlined.
    And look, it only took several thousands of words to explain why it only takes about 15 lines of code to parse expressions once you have decided upon your language and written your tokenizer.
    Enjoy

  • Binary tree for parsing algebraic expressions

    hi all
    i have a project to do.. i need to read in an algebraic expression, parse it and form a binary tree. later use that tree to evaluate the expressions when the actual value for those variables are passed..
    can anyone tell me how to go about doing this.
    im a beginner in java..
    tks

    first of, your going to need three stacks. The implementation of a stack in JAVA is something that's easy to find, do a forum search. Your going to need a Binary Node as well. One stack will hold the operator elements of your algebraic expression, another will hold the numeric values, and the final will hold your binary nodes:
    expression: 4 + 6 - (6 * 9)
    now there are some rules that should be followed. Whenever a closing parenthesis is encountered ")", pop an operator, pop two numbers, and create the corresponding binary node.
    order of operations:
    push 4 onto numberStack
    push + onto operatorStack
    push 6 onto numberStack
    push - onto operatorStack
    push ( onto operator Stack
    push 6 onto numberStack
    push * onto operatorStack
    push 9 onto numberStack
    ")" encountered
    Now, your stacks look something like this:
    operatorStack: [ +, -, (, *, ) ]
    numberStack: [ 4, 6, 6, 9 ]
    pop 9 and 6 from numberStack, pop * from operatorStack
    build a binaryNode using these three elements
    push this node onto the binaryNodeStack
    I.E. using the expression above:
    / \ is the resulting binary node once the ")" is received.
    6 9
    etc.. there are some rules regarding when to pop and build binary nodes, it's been a while since i did this assignment and frankly my brain hurts, hope this gets you started, good luck.

  • NEED HELP:To Parse Conditional Operator Within An Expression.

    Hi there:
    I'm having some difficulties to parse some conditoional operator. My requirements is:
    Constants value: int a=1,b=2,c=3
    Input/Given value: 2
    conditional operator expression: d=a|b|c
    Expected result: d=true
    Summary: I like to receive a boolean from anys expressions defined, which check against Input/Given value.Note: The expression are various from time to time, based on user's setup, the method is smart enough to return boolean based on any expression.
    Let me know if you have any concerns.
    Thanks a million,
    selena

    here is a simple example.
    BNF changes
    EXPR ::= OPERATOR
    EXPR ::= OPERATION OPERANT EXPR
    This is as far as I can go, please use it as a template only
    because you need to take into account the precedence using ()
    the logic of the eval was simply right to left so I think it is not what you want
    Cheers
    public interface Expression
         String OR = "|";
         String AND = "&";
         public boolean eval(int value) throws Exception;
    public class ExpressionImpl implements Expression
         public String oper1;
         public String operant;
         public Expression tail;
         /* (non-Javadoc)
          * @see parser.Expression#eval()
         public boolean eval(int value) throws Exception
              int val1 = getValue(oper1);
              if (null == tail)
    System.out.println(val1 + ":" + value);               
                   return (val1 == value);
              else
                   if (OR.equals(operant))
                        return (val1 == value || tail.eval(value));
                   else if (AND.equals(operant))
                        return (val1 == value && tail.eval(value));
                   else
                        throw new Exception("unsupported operant " + operant);
         private int getValue(String operator) throws Exception
              Integer temp = ((Integer)Parser.symbol_table.get(operator));
              if (null == temp)
                   throw new Exception("symbol not found " + operator);
              return temp.intValue();
         public String toString()
              if (null == operant) return oper1;
              return oper1 + operant + tail.toString();
    public class Parser
         public static HashMap symbol_table = new HashMap();
          * recursive parsing
         public Expression parse(String s)
              ExpressionImpl e = new ExpressionImpl();
              e.oper1 = String.valueOf(s.charAt(0));
              if (s.length() == 1)
                   return e;
              else if (s.length() > 2)
                   e.operant = String.valueOf(s.charAt(1));
                   e.tail = parse(s.substring(2));
              else
                   throw new IllegalArgumentException("invalid input " + s);
              return e;
         public static void main(String[] args) throws Exception
              Parser p = new Parser();
              Parser.symbol_table.put("a", new Integer(1));
              Parser.symbol_table.put("b", new Integer(2));
              Parser.symbol_table.put("c", new Integer(3));
              Parser.symbol_table.put("d", new Integer(4));
              Expression e = p.parse("a|b|c&d");
              System.out.println("input " + e.toString());
              System.out.println(e.eval(2));
    }

  • Simulating a program like Outlook express for a Online Store

    Hi all,
    I want to create a program that should read all the information come to me on a website using my user name and password. Some times i need to edit or add information on the site. The website will have sessions and cookies enabled. I don't know how to do it. Any help in any application is greatly appreciated.
    I have a new project assigned to me. That is for my inhouse needs, I need to update the data in a site . This site will give a username and password to all the members. We are one of the members. But we have to simulate a tool to update the products in their site.
    It will be something like outlook express but not a mailing interface.
    Let me explain outlook first. Out look is getting the username and password from the user and then logs in into the users account in the website (yahoo or hotmail anything). And then they are going to the mail section and taking the mails if there were any. Same thing for composing a mail.
    But I am not considered with mails. Here the data is like product information. They may already be there, or they may be new items. I will have to keep a local database here and to synchronize this database with the website.

    Hi again.
    Well, your headed for a lot of trouble. I sugest you try to find and HTTP library and and HTML parsing library.
    If HTTP library should allow you to make the request and the HTML parsing to save the trouble of writing a parser. But you will need to know wher to find in the parsed structure the data you need. This is the problem.
    The Swing package contains an HTML parsing system: check the package javax.swing.text.html adn javax.swing.text.html.parser. Maybe these will do for the parsing and the Gets, the post I don't know...
    The experience I have with this was in building a small web server, not a client :-)
    Hope this helps,
    Nuno

  • SSRS Dataset Filter expression query

    I have an SSRS report which has a simple dataset and accepts two date parameters, and filters the results based on a Position parameter. The Position parameter filter is:
    Expression: Position
    Operator: In
    Value: [@Position]
    I have now been told that each position has a few other aliases in the database against the Position column. i.e. For a Position of "Contractor" I now also need to return "Consultant" records, based on the same field. Note, only one Position
    can be selected at a time.
    I imagine I have to update the Value expression above to do something like:
    - When @Position in "Contractor" also return rows where Position is x, y, z (I am happy to hard code these)
    I have about 10 Positions each with one or more aliases - can someone help guide me on how to set up the expression?
    Thanks!

    Thanks Ione.
    Ok - I have set my parameter values in the value box, comma separated
    My dataset filter for the param Operator is set to IN and the value [@Position]
    However where I have multiple values set against a position I do not get any results (there is data in the DB)
    Do I need to do something different when setting the paramter values? i.e. use the expression and then a function to parse the comman separted values
    Or - something in the Filter to make the expression parse the incoming paramter?
    Thanks again

Maybe you are looking for