Code Monkey home page Code Monkey logo

pascal-parser-yacc's Introduction

Pascal Parser using Bison/YACC

Bison/YACC based parser for (Mini-)Pascal that is able to print out the AST in textual form (in a somewhat LISP like syntax).

工具准备

我就试了macOS,window和linux看着办

下载flexbison

brew install flex
brew install bison

Usage

There is a Makefile that calls bison and flex on the corresponding files (parser.y and parser.l, respectively) and afterwards compiles the resulting code with g++. Therefore you can just call make to build this program:

make

Example output

Given this input code:

program minipas;
  var a,y: integer;
      b, i: integer;
      x: array [1..100] of real;

  function gcd (a, b: integer) : integer;
  begin
    while a*b <> 0 do
    begin
      if a > b then
        a := a-b
      else b := b-a;
    end;
    if a = 0 then
      gcd := b
    else gcd := a;
  end;


  function factorial (a: integer) : integer;
  var k, fact: integer;
  begin
    fact := 1;
    k := 2;
    while k <= a do
    begin
      fact := fact*k;
      k := k+1;
    end;
    factorial := fact;
  end;

  function sum (n: integer) : integer;
  var s, k: integer;
  begin
    s := 0;
    k := 1;
    while k <= n do
    begin
      sum := sum+k;
      k := k+1;
    end;
    if s <> n*(n+1)/2 then
      sum := -1
    else sum := s;
  end;

the parser spits out following text representation of the AST:

(program minipas (defs  (a: integer) (y: integer) (b: integer) (i: integer) (x: real[1..100]))

(method gcd (args (a: integer) (b: integer) (defs)
((while (<> (* a b) 0) (do ((if (> a b) (then (assign a := (- a b))) (else (assign b := (- b a)))))))(if (= a 0) (then (assign gcd := b)) (else (assign gcd := a))))

(method factorial (args (a: integer) (defs (k: integer) (fact: integer))
((assign fact := 1)(assign k := 2)(while (<= k a) (do ((assign fact := (* fact k))(assign k := (+ k 1)))))(assign factorial := fact))

(method sum (args (n: integer) (defs (s: integer) (k: integer))
((assign s := 0)(assign k := 1)(while (<= k n) (do ((assign sum := (+ sum k))(assign k := (+ k 1)))))(if (<> s (/ (* n (group (+ n 1))) 2)) (then (assign sum := 1)) (else (assign sum := s))))

pascal-parser-yacc's People

Contributors

zyl-df avatar

Watchers

 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.