A Flex/Bison project that parses and validates a simplified Android XML layout language.
The project was developed for an undergraduate course in the Department of Computer Engineering and Informatics at the University of Patras. It uses Flex for lexical analysis and Bison for grammar parsing and semantic validation.
This repository implements a command-line parser for Android-like XML layout files.
The program reads an input file, tokenizes it using Flex, parses it using Bison, and checks whether the file follows the supported layout rules. If the input is valid, the program prints a success message. If there is a syntax or semantic error, it reports the line where the problem was detected and prints an explanatory message.
The project should be described as a parser or validator for a subset of Android XML layouts, not as a complete Android application generator.
The validator supports a simplified XML structure based on common Android UI components.
Supported layout containers:
LinearLayoutRelativeLayout
Supported UI elements:
TextViewImageViewButtonRadioGroupRadioButtonProgressBar
Supported Android-style attributes include:
android:idandroid:layout_widthandroid:layout_heightandroid:orientationandroid:textandroid:textColorandroid:srcandroid:paddingandroid:button_countandroid:checkedButtonandroid:maxandroid:progress
The parser performs both syntactic and semantic checks.
A valid input file must start with either a LinearLayout or a RelativeLayout.
Both layout types must include:
android:layout_widthandroid:layout_height
LinearLayout can also include an android:orientation attribute.
The parser checks for missing required attributes and duplicate attributes.
A TextView must include:
android:layout_widthandroid:layout_heightandroid:text
Optional attributes include:
android:idandroid:textColor
An ImageView must include:
android:layout_widthandroid:layout_heightandroid:src
Optional attributes include:
android:idandroid:padding
A Button must include:
android:layout_widthandroid:layout_heightandroid:text
Optional attributes include:
android:idandroid:padding
A RadioGroup must include:
android:layout_widthandroid:layout_heightandroid:button_count
The parser also checks that:
- The value of
android:button_countmatches the number of nestedRadioButtonelements. - The value of
android:checkedButton, when used, matches the ID of one of the nested radio buttons.
Each RadioButton must include:
android:layout_widthandroid:layout_heightandroid:text
A ProgressBar must include:
android:layout_widthandroid:layout_height
The parser also checks that:
android:maxandandroid:progressmust either both exist or both be absent.android:progresscannot be greater thanandroid:max.
The parser checks that ID values are unique across the input file.
If the same ID value is used more than once, the program reports an error.
For android:layout_width and android:layout_height, the accepted values are:
- A positive integer
"wrap_content""match_parent"
The project uses a simplified value format and does not fully support all Android XML value types such as @+id/..., @string/..., color resources, dimensions, namespaces, or XML headers.
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" />
<Button android:layout_width="100" android:layout_height="50" android:text="Submit" />
</LinearLayout>android-xml-layout-validator/
├── myparser.l # Flex lexical analyzer
├── myparser.y # Bison grammar and semantic validation rules
├── lex.yy.c # Generated C file from Flex
├── myparser.tab.c # Generated C parser from Bison
├── myparser.tab.h # Generated Bison header file
├── config.txt # Sample input file
└── run.exe # Precompiled Windows executable
| Technology | Purpose |
|---|---|
| C | Generated parser implementation and semantic actions |
| Flex | Lexical analysis/token generation |
| Bison | Grammar parsing and syntax validation |
| GCC | Compilation |
Install Flex, Bison, and GCC.
Then generate and compile the parser:
flex myparser.l
bison -d myparser.y
gcc lex.yy.c myparser.tab.c -o android_xml_validatorOn Windows, you can compile to an .exe file:
gcc lex.yy.c myparser.tab.c -o run.exeRun the validator by passing an input file as an argument:
./android_xml_validator input.xmlOn Windows:
run.exe input.xmlIf the file is valid, the program prints a message that the input file was correctly written.
If the file contains an error, the program prints the input and reports the line where the error was detected.
This project validates a simplified XML-like language and does not support the full Android XML layout specification.
Current limitations include:
- No support for XML declarations or namespace declarations
- No support for standard Android resource references such as
@+id/name - No support for complex values such as colors, dimensions, strings, or drawables
- Limited set of supported UI components
- Generated files and a compiled executable are included in the repository
- No automated test suite or example collection of valid and invalid inputs
This project was developed for academic purposes.