Giter VIP home page Giter VIP logo

parsertest's Introduction

Parser Test with Xcode

Xcode can handle bison and flex definition files. Xcode can even compile the generated files as Objective-C making it easier to bridge a parser into Objective-C. Pretty nice I think.

This demo shows a simple calculator. tokenizer.lm contain the parts for divinding the character stream into parts for the parser which is in the parser.ym file. The added m on the extension is that detail that makes Xcode compile the generated files as Objective-C files.

To make it a little bit more interesting I added identifiers that is dynamically handled between the tokenizer and parser. In the tokenizer you can see that it will be set as a retained string. The parser only knows about pi at the moment and everything else will have the value 0.0

There are two possible places for a identifier string to be released, either in the last part of this rule

number : INTEGER { $$ = $1; }
| FLOAT { $$ = $1; }
| IDENTIFIER { if ([$1 isEqualToString:@"pi"]) $$ = M_PI; else $$ = 0.0; [$1 release]; }

or the defined destructor

%destructor { [$$ release]; } IDENTIFIER 

The defined destructor is used if the parser need to clean up some intermediate handling, by error or not.

One caveat I stumbled upon was that I couldn't get "[[" to work in the tokenizer, which seem to use that for some internal marker. So instead I use "[ [" which may look strange. The line in the tokenizer is this

[a-zA-Z]+ { yylval.identifier = [ [NSString stringWithFormat:@"%s", yytext] retain]; return IDENTIFIER; }

Now I wish I had a task to solve with my own language...

parsertest's People

Contributors

epatel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.