/* * expr ::= term + expr | term * term ::= factor * term | factor * factor ::= num | (expr) */ #include <stdio.h> int tokennext (); void tokenback (); void expr (); void term (); void factor (); char *s = "(4+3)*2"; int i = 0; int tokennext () { return s [i++]; } void tokenback () { i--; } void expr () { term (); if (tokennext () == '+') { expr (); printf ("add \$r1, \$r1, \$r2n"); } else tokenback (); return; } void term () { factor (); if (tokennext () == '*') { term (); printf ("mult \$r2, \$r2, \$r3n"); } else tokenback (); return; } void factor () { int ch; ch = tokennext (); if ((ch >= '0') \&\& (ch <= '9')) printf ("li \$r3, %cn", ch); else if (ch == '(') { expr (); printf ("move \$r1, \$r3n"); } return; } int main (void) { printf (".textn"); printf ("main:n"); expr (); return 0; }
An den Registern müssen wir noch arbeiten
david@laptop-peaq:~\$ gcc bakusnaurrmips01.c david@laptop-peaq:~\$ ./a.out .text main: li \$r3, 4 li \$r3, 3 add \$r1, \$r1, \$r2 move \$r1, \$r3 david@laptop-peaq:~\$