Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

program to display terminal symbols ina grammar

+6 votes
asked Jan 3, 2020 by anonymous

1 Answer

0 votes
answered May 27, 2025 by A V S Manvith (220 points)
def extract_terminals(grammar):
    non_terminals = set(grammar.keys())
    terminals = set()

    for production in grammar.values():
        for rule in production:
            for symbol in rule:
                if not symbol.isupper() and symbol not in ['→', '|', 'ε']:
                    terminals.add(symbol)

    return terminals

# Example grammar (as a dictionary)
# A → aB | b
# B → c | ε
grammar = {
    "A": ["aB", "b"],
    "B": ["c", "ε"]
}

terminals = extract_terminals(grammar)
print("Terminal symbols:", terminals)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...