This is a prettier for C code. It has been developed only for a small subset of the grammar, explained in the next sections.
For example, this piece of code to calculate if a number is prime:
#include
<stdio.h>
/* Program to calculate if a number given is prime */
int main ( ){
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i=2;i<=n/2;++i) {
// condition for non-prime
if (n% i == 0)
{
flag = 1;break; }
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else{
if (flag == 0) printf("%d is a prime number.", n);
else printf("%d is not a prime number.", n);
}
return 0;
}
would be reformatted as:
#include <stdio.h>
/* Program to calculate if a number given is prime */
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i)
{
// condition for non-prime
if (n % i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
To compile and run you can use the Makefile:
make
make run # Execute over the file specified as TEST, redirecting the input
make run2 # Execute reading the file specified as TEST
The output will be save in a file called _output.c.
Not all the grammar has been considered for this project, just a little subset:
-
Types: char, int, long, float, double, pointers and arrays. Modifiers as global, auto... or long long, unsigned int... are not allowed.
-
Preprocessing directives: #include, #ifdef/#ifndef...#endif, #define
-
Simple statements: expressions, declarations, asignations, break, continue and return.
-
Complex statements: if, if-else, for, while, do-while and function definitions
-
Multiline or single line comments
- prettier.l: lexical parser
- prettier.y: syntactic parser
- tests: check the correct behaviour in specific situations
- test_errors: check the correct diagnosis of syntax errors
- test1.c: main not found
- test2.c: conditional and repetitive structures outside a function
- test3.c: expression expected
- test4.c: incorrect if/for/while/do-while header
- test5.c: ';' expected
- test6.c: unrecognized preprocessing directive
- test7.c: break or continue statement outside a loop
- test8.c: return outside a function