Posts Tagged ‘yacc’

ogc 4.1 – Floating But Not Sinking

August 3rd, 2010 subogero Comments off

Erm… ogc 4.0 had some certain erm… bugs in its syntax.

But ogc 4.1 is now downloadable from the ogc page. Including native Linux and Windows binaries.

ogc – The OG Calculator

July 29th, 2009 subogero Comments off

So my yacc studies led me to a series of thoughts about writing my own command-line calculator with integer arithmetics. Which the Windows Calculator is not.

I set the following goals:

  • 32-bit integer arithmetics (don’t ask me why, I just need it at work, OK?)
  • decimal, octal and hexadecimal formats
  • unlimited undo
  • a fair amount of memory registers
  • ability to continue from the last result (+5 should add 5 to the last result)
  • no mouse clicking
  • following Unix/GNU conventions

The program’s called ogc. I wrote it in yacc surprisingly quickly. The related info and the sources are available on my dedicated ogc page.

Categories: posts Tags: , , ,

yacc – Yet Another Compiler Compiler

July 29th, 2009 subogero Comments off

While reading esr’s book about UNIX and laziness, I found it referring to a mysterious “yacc” on many occasions. As if sed, awk, tr, ls, sudo et al were not confusing enough. I just could not resist and googled it. Voilá.

It turned out it was a high-level programming language to describe the grammar of, erm, a language, and parse it. It was created by Stephen C. Johnson back in 1973! The paper contains an appendix with an example of a simple command-line calculator.

Just try to imagine the horrors of parsing and executing simple mathematical expressions with a program written in the C language. For instance “-3*(5/2)-(5*-8)+1″.
Operator precedence, brackets, telling unary minus (-3) from the normal minus (6-2). I’d never try it.

But in yacc, the essence of the problem is solved in a few lines:

expr :    '('  expr  ')'
               {    $$  =  $2;  }
     |    expr  '+'  expr
               {    $$  =  $1  +  $3;  }
     |    expr  '-'  expr
               {    $$  =  $1  -  $3;  }
     |    expr  '*'  expr
               {    $$  =  $1  *  $3;  }
     |    expr  '/'  expr
               {    $$  =  $1  /  $3;  }
     |    '-'  expr        %prec  UMINUS
               {    $$  =  -  $2;  }
     |    number

This led me to a whole series of thoughts. But more of that next time…

Anyway, if yacc is yet another compiler-compiler, which was the previous compiler compiler?

Categories: posts Tags: