Spain and Portugal signed the treaty formally joining the 10-nation European Community in solemn ceremonies in Lisbon and Madrid today. But the ceremonies here were marred by violence as Basque separatists killed four persons in Madrid and Bilbao.

Three assailants, two men and a woman, fatally shot an Army colonel and his driver in Madrid early this morning, while Spanish Prime Minister Felipe Gonzalez was attending the day's first ceremonies in Lisbon. The killers then abandoned their getaway car, packed with more than 20 pounds of explosives, in a department store garage, where a policeman was killed and seven others were wounded when trying to deactivate the bomb.

Security services said the car-bomb tactic and the type of ammunition used indicated beyond doubt that the Basque separatist organization ETA was responsible.

Recent statements by ETA, whose initials stand for Basque Homeland and Liberty, have made clear its opposition to Spain's entry into the European Community, which the Marxist ETA has labeled "a capitalist conspiracy."

Later today, in a suburb of the Basque industrial port of Bilbao, a Navy noncommissioned officer was shot to death by a gunman at close range.

The violence in Madrid occurred despite massive security precautions to protect European leaders arriving for today's ceremony. Officials said more than 2,000 police and paramilitary civil guards had been deployed in the capital.

At the Lisbon ceremony, held in the 16th-century Jeronimos monastery where Portuguese explorers used to pray before their voyages, Prime Minister Mario Soares signed the treaty of membership for Portugal in a brief respite from the political crisis caused by the walkout of his Social Democratic partners from his Socialist-led coalition.

"We have made it," exclaimed Soares, who had been instrumental in applying for membership in 1977 during an earlier term as prime minister. Soares said the signing symbolized the belated end of Portugal's imperial past and the beginning of a modern future in Europe.

In Madrid, the treaty was signed in the 18th-century Oriente royal palace, now a museum used occasionally for state events. The chandelier-lit Hall of Columns is dominated by a statue of Emperor Charles V, who ruled here when Spain was ascendant in Europe.

"Spain," said King Juan Carlos, "has never wished to stop belonging to Europe."

Gonzalez and Soares, with representatives of each member state and top officials of the European Commission, put their signatures to a 1,000-page document that constitutes the enlargement treaty. Attending both ceremonies were European prime ministers, including France's Laurent Fabius and Italy's Bettino Craxi, and foreign ministers, among them Britain's Geoffrey Howe and West Germany's Hans-Dietrich Genscher.

From the original six member states that signed the Treaty of Rome in 1957, the European Community now will embrace all of Western Europe except Norway, Sweden, Austria and Switzerland. Spanish and Portuguese membership will not actually begin until Jan. 1, after ratification by the parliaments of the 10 present members.

Portugal will be by far the poorest nation in the enlarged Common Market, with a negative growth rate and inflation of more than 24 percent. In theory, it should benefit from membership through grants that will help it modernize its industrial and agricultural structures.

Spain, with the community's fifth largest population, ranks behind Ireland in wealth but ahead of Greece and Portugal. Spain's problem is a huge unemployment rate, close to 23 percent of the active working population, and an economy that will be hard hit by the tough terms of its entry, which include rapid reduction of its high industrial tariffs and a 10-year transition before its agricultural produce can be traded on duty-free terms with its European partners.