Archive

Archive for July, 2009

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:

Master Foo

July 27th, 2009 subogero Comments off

Two must-reads:

Master Foo

Master Foo

The Tao of Programming

Categories: posts Tags: ,

The Art of UNIX Programming

July 25th, 2009 subogero Comments off

It’s not what I do.
It’s the title of a book.

I found it on the internet during my initial sed adventures. The author is Eric Steven Raymond. It’s absolutely brilliant. I have been working with embedded automotive software for ages. And although I’ve never had much to do with Unix, this book pretty much sums up everything I’ve come to believe in:

Keep it simple, stupid!

Do one thing well.

Separate policy from mechanism.

Write programs to write programs.

Text rulez.

Working programs before specs and optimisation.

Fold knowledge into data, use stupid algorithms.

Design for the future.

Distrust all claims for the One True Way.

Constructive laziness is the cardinal virtue of the master programmer.

Read more…

Categories: posts Tags: , ,

Seduced by sed

July 25th, 2009 subogero Comments off

So I started searching the internet about the usage of sed, the ultimate stream editor. I found the number one site about the Awful Truth. If you have any question about sed, you’ll find the answer here. My favourites so far:

Simple replacement of “Windows” to “Linux”, sorry “foo” to “bar” with the magic s (substitution), go for all occurrences this time with flag g (global).

sed -e 's/foo/bar/g'

Use any other delimiter instead of slash, in this case the comma, if slash is used in any pattern.
Keep a part of a matched string defined by the brakets \( \) and using \1.
Let’s convert “c:” to “/cygdrive/c”.

sed -e 's, \([A-Za-z]\):, /cygdrive/\1,g'

You will also notice the presence of the dreaded regular expression above. I also learned that sed is a so-called filter. In streams the standard input, so sed takes the text you type, and writes the result to the terminal window.

And now comes the best part, at least for a rookie like me, the redirection wizardry. You can redirect sed’s input or output to any other file, or even any other program’s standard input or output.

For instance let’s copy the contents of foo.txt to bar.txt, but will all occurrences of “foo” replaced with “bar”.

sed -e 's/foo/bar/g' <foo.txt >bar.txt

Same operation but replace in the same file, foo.txt.

sed -e 's/foo/bar/g' -i foo.txt

And using the pipe and the command-quote you can do the same between environment variables, as well. The pipe “|” connects the output of the program on its left to the input of the one on its right. And the command quote ` ` redirects the final output to a variable.

BAR=`echo $FOO | sed -e 's/foo/bar/g'`

I think it’s obvious by now that I’m infected. I use the words “foo” and “bar”.

Categories: posts Tags:

cygwin – How it All Started

July 25th, 2009 subogero Comments off

At the very respectable company where I spend my days, we’ve been using a mysterious cygwin shell for building software projects. Don’t ask me why. I’d been wondering what the bash2.02> prompt was supposed to mean. Sometimes I overheard sentences about how good the GNU make was, or how cool it was to pipe sed into tr, but it’d be overstating the facts that I understood them.

Considering myself an embedded software wizard? Check.
Understanding our makefile? Nope.

Until the day came, when upgrading to the newest version of cygwin was proposed to achieve compatibility with a code-checking tool. And the loser to do this job was yours truly. So I upgraded and, guess what, our make ceased to work. After consulting some gurus in the office and the internet it turned out that cygwin had stopped supporting Win32 path formats altogether. Fantastic.

My beloved drive letters (C:\) were blown out of the window.
In came a weird /cygdrive/c/ format.
In case of a Unix-hacker, the terms root directory and mount-point spring to the lips.

It was suggested that I could fix this with the bloody sed in a matter of a few seconds, thank you very much.

And so it started…

Categories: posts Tags: ,

Welcome to #!/bin/bash

July 24th, 2009 subogero Comments off

This blog is about my adventures in the weird and wonderful world of Unix, Linux, GNU, cygwin, Ubuntu, the Bourne Again Shell, yacc, makefiles and similar terrible things.

I mean, who would not run for shelter when confronted with this:

$(MK_TMP)/%.dep: %.c
  @$(CC) $< $(CFLAGS1) $(INCDIRS) $(CDEFS) -Em 2>>$(LOG) \
  | sed -e '/\.\c/d'                                     \
        -e '\%$(INC_TMP)%d'                              \
        -e 's,\($*\).obj[ :]*,$(OBJ_TMP)/\1.o $@ :,g'    \
  | tr -d '\r' > $@ 2>$(@:.dep=.log)
Categories: posts Tags: